blob: b4d0a0d6a109c84b7274ec5b08f7aa72db825676 [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
Harald Weltef2949742021-01-20 14:54:14 +010011#define LOGNSE(nse, lvl, fmt, args ...) \
12 LOGP(DLNS, lvl, "NSE(%05u) " fmt, (nse)->nsei, ## args)
13
14#define LOGBIND(bind, lvl, fmt, args ...) \
15 LOGP(DLNS, lvl, "BIND(%s) " fmt, (bind)->name, ## args)
16
17
18#define LOGNSVC(nsvc, lvl, fmt, args ...) \
19 do { \
20 if ((nsvc)->nsvci_is_valid) { \
21 LOGP(DLNS, lvl, "NSE(%05u)-NSVC(%05u) " fmt, \
22 (nsvc)->nse->nsei, (nsvc)->nsvci, ## args); \
23 } else { \
24 LOGP(DLNS, lvl, "NSE(%05u)-NSVC(none) " fmt, \
25 (nsvc)->nse->nsei, ## args); \
26 } \
27 } while (0)
28
Alexander Couzens6a161492020-07-12 13:45:50 +020029struct osmo_fsm_inst;
30struct tlv_parsed;
31struct vty;
32
33struct gprs_ns2_vc_driver;
34struct gprs_ns2_vc_bind;
35
Alexander Couzens90ee9632020-12-07 06:18:32 +010036#define NS_TIMERS_COUNT 10
Harald Welte3a44d172020-12-16 12:02:51 +010037#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 +020038#define NS_TIMERS_HELP \
39 "(un)blocking Timer (Tns-block) timeout\n" \
40 "(un)blocking Timer (Tns-block) number of retries\n" \
41 "Reset Timer (Tns-reset) timeout\n" \
42 "Reset Timer (Tns-reset) number of retries\n" \
43 "Test Timer (Tns-test) timeout\n" \
44 "Alive Timer (Tns-alive) timeout\n" \
45 "Alive Timer (Tns-alive) number of retries\n" \
Alexander Couzens90ee9632020-12-07 06:18:32 +010046 "SNS Provision Timer (Tsns-prov) timeout\n" \
47 "SNS Size number of retries\n" \
48 "SNS Config number of retries\n" \
Alexander Couzens6a161492020-07-12 13:45:50 +020049
50/* Educated guess - LLC user payload is 1500 bytes plus possible headers */
51#define NS_ALLOC_SIZE 3072
52#define NS_ALLOC_HEADROOM 20
53
54enum ns2_timeout {
55 NS_TOUT_TNS_BLOCK,
56 NS_TOUT_TNS_BLOCK_RETRIES,
57 NS_TOUT_TNS_RESET,
58 NS_TOUT_TNS_RESET_RETRIES,
59 NS_TOUT_TNS_TEST,
60 NS_TOUT_TNS_ALIVE,
61 NS_TOUT_TNS_ALIVE_RETRIES,
62 NS_TOUT_TSNS_PROV,
Alexander Couzens90ee9632020-12-07 06:18:32 +010063 NS_TOUT_TSNS_SIZE_RETRIES,
64 NS_TOUT_TSNS_CONFIG_RETRIES,
Alexander Couzens6a161492020-07-12 13:45:50 +020065};
66
67enum nsvc_timer_mode {
68 /* standard timers */
69 NSVC_TIMER_TNS_TEST,
70 NSVC_TIMER_TNS_ALIVE,
71 NSVC_TIMER_TNS_RESET,
72 _NSVC_TIMER_NR,
73};
74
Harald Welte76346072021-01-31 11:54:02 +010075enum ns2_vc_stat {
Alexander Couzens6a161492020-07-12 13:45:50 +020076 NS_STAT_ALIVE_DELAY,
77};
78
Harald Welte76346072021-01-31 11:54:02 +010079enum ns2_bind_stat {
80 NS2_BIND_STAT_BACKLOG_LEN,
81};
82
Alexander Couzens6a161492020-07-12 13:45:50 +020083/*! Osmocom NS2 VC create status */
Alexander Couzensba5a9922021-01-25 16:03:23 +010084enum ns2_cs {
85 NS2_CS_CREATED, /*!< A NSVC object has been created */
86 NS2_CS_FOUND, /*!< A NSVC object has been found */
87 NS2_CS_REJECTED, /*!< Rejected and answered message */
88 NS2_CS_SKIPPED, /*!< Skipped message */
89 NS2_CS_ERROR, /*!< Failed to process message */
Alexander Couzens6a161492020-07-12 13:45:50 +020090};
91
Harald Welte3d5eaee2021-01-30 21:33:02 +010092enum ns_ctr {
93 NS_CTR_PKTS_IN,
94 NS_CTR_PKTS_OUT,
Harald Weltef22ae5a2021-01-30 22:01:28 +010095 NS_CTR_PKTS_OUT_DROP,
Harald Welte3d5eaee2021-01-30 21:33:02 +010096 NS_CTR_BYTES_IN,
97 NS_CTR_BYTES_OUT,
Harald Weltef22ae5a2021-01-30 22:01:28 +010098 NS_CTR_BYTES_OUT_DROP,
Harald Welte3d5eaee2021-01-30 21:33:02 +010099 NS_CTR_BLOCKED,
Harald Welteb40bf8b2021-01-30 22:43:01 +0100100 NS_CTR_UNBLOCKED,
Harald Welte3d5eaee2021-01-30 21:33:02 +0100101 NS_CTR_DEAD,
102 NS_CTR_REPLACED,
103 NS_CTR_NSEI_CHG,
104 NS_CTR_INV_VCI,
105 NS_CTR_INV_NSEI,
106 NS_CTR_LOST_ALIVE,
107 NS_CTR_LOST_RESET,
108};
Alexander Couzens6a161492020-07-12 13:45:50 +0200109
110#define NSE_S_BLOCKED 0x0001
111#define NSE_S_ALIVE 0x0002
112#define NSE_S_RESET 0x0004
113
114#define NS_DESC_B(st) ((st) & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED")
115#define NS_DESC_A(st) ((st) & NSE_S_ALIVE ? "ALIVE" : "DEAD")
116#define NS_DESC_R(st) ((st) & NSE_S_RESET ? "RESET" : "UNRESET")
117
118/*! An instance of the NS protocol stack */
119struct gprs_ns2_inst {
120 /*! callback to the user for incoming UNIT DATA IND */
121 osmo_prim_cb cb;
122
123 /*! callback data */
124 void *cb_data;
125
126 /*! linked lists of all NSVC binds (e.g. IPv4 bind, but could be also E1 */
127 struct llist_head binding;
128
129 /*! linked lists of all NSVC in this instance */
130 struct llist_head nse;
131
Alexander Couzens6a161492020-07-12 13:45:50 +0200132 uint16_t timeout[NS_TIMERS_COUNT];
133
134 /*! workaround for rate counter until rate counter accepts char str as index */
Harald Welte97ccbf72021-01-31 11:49:19 +0100135 uint32_t nsvc_rate_ctr_idx;
Harald Welte76346072021-01-31 11:54:02 +0100136 uint32_t bind_rate_ctr_idx;
Harald Welte53a2fde2020-12-01 22:21:14 +0100137
138 /*! libmnl netlink socket for link state monitoring */
139 struct osmo_mnl *linkmon_mnl;
Alexander Couzens6a161492020-07-12 13:45:50 +0200140};
141
Harald Welte53a2fde2020-12-01 22:21:14 +0100142
Alexander Couzens6a161492020-07-12 13:45:50 +0200143/*! Structure repesenting a NSE. The BSS/PCU will only have a single NSE, while SGSN has one for each BSS/PCU */
144struct gprs_ns2_nse {
145 uint16_t nsei;
146
147 /*! entry back to ns2_inst */
148 struct gprs_ns2_inst *nsi;
149
150 /*! llist entry for gprs_ns2_inst */
151 struct llist_head list;
152
153 /*! llist head to hold all nsvc */
154 struct llist_head nsvc;
155
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100156 /*! count all active NSVCs */
157 int nsvc_count;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100158
Alexander Couzens6a161492020-07-12 13:45:50 +0200159 /*! true if this NSE was created by VTY or pcu socket) */
160 bool persistent;
161
Alexander Couzensda0a2852020-10-01 23:24:07 +0200162 /*! true if this NSE wasn't yet alive at all.
163 * Will be true after the first status ind with NS_AFF_CAUSE_RECOVERY */
164 bool first;
165
Alexander Couzens6a161492020-07-12 13:45:50 +0200166 /*! true if this NSE has at least one alive VC */
167 bool alive;
168
Alexander Couzensaac90162020-11-19 02:44:04 +0100169 /*! which link-layer are we based on? */
170 enum gprs_ns2_ll ll;
171
Alexander Couzensd923cff2020-12-01 01:03:52 +0100172 /*! which dialect does this NSE speaks? */
173 enum gprs_ns2_dialect dialect;
174
Alexander Couzens6a161492020-07-12 13:45:50 +0200175 struct osmo_fsm_inst *bss_sns_fi;
Harald Welte3221f872021-01-18 14:58:51 +0100176
Alexander Couzense052c412021-02-15 02:10:57 +0100177 /*! sum of all the data weight of _alive_ NS-VCs */
Harald Welte3221f872021-01-18 14:58:51 +0100178 uint32_t sum_data_weight;
179
Alexander Couzense052c412021-02-15 02:10:57 +0100180 /*! sum of all the signalling weight of _alive_ NS-VCs */
Harald Welte3221f872021-01-18 14:58:51 +0100181 uint32_t sum_sig_weight;
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100182
183 /*! MTU of a NS PDU. This is the lowest MTU of all NSVCs */
184 uint16_t mtu;
Harald Welte5b034fb2021-03-04 14:16:49 +0100185
186 /*! are we implementing the SGSN role? */
187 bool ip_sns_role_sgsn;
Alexander Couzens6a161492020-07-12 13:45:50 +0200188};
189
190/*! Structure representing a single NS-VC */
191struct gprs_ns2_vc {
192 /*! list of NS-VCs within NSE */
193 struct llist_head list;
194
195 /*! list of NS-VCs within bind, bind is the owner! */
196 struct llist_head blist;
197
198 /*! pointer to NS Instance */
199 struct gprs_ns2_nse *nse;
200
201 /*! pointer to NS VL bind. bind own the memory of this instance */
202 struct gprs_ns2_vc_bind *bind;
203
204 /*! true if this NS was created by VTY or pcu socket) */
205 bool persistent;
206
207 /*! uniquely identifies NS-VC if VC contains nsvci */
208 uint16_t nsvci;
209
210 /*! signalling weight. 0 = don't use for signalling (BVCI == 0)*/
211 uint8_t sig_weight;
212
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100213 /*! signalling packet counter for the load sharing function */
214 uint8_t sig_counter;
215
216 /*! data weight. 0 = don't use for user data (BVCI != 0) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200217 uint8_t data_weight;
218
219 /*! can be used by the bind/driver of the virtual circuit. e.g. ipv4/ipv6/frgre/e1 */
220 void *priv;
221
222 bool nsvci_is_valid;
Harald Weltec962a2e2021-03-05 08:09:08 +0100223 /*! should this NS-VC only be used for SNS-SIZE and SNS-CONFIG? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200224 bool sns_only;
225
226 struct rate_ctr_group *ctrg;
227 struct osmo_stat_item_group *statg;
228
Alexander Couzens6a161492020-07-12 13:45:50 +0200229 enum gprs_ns2_vc_mode mode;
230
231 struct osmo_fsm_inst *fi;
232};
233
234/*! Structure repesenting a bind instance. E.g. IPv4 listen port. */
235struct gprs_ns2_vc_bind {
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100236 /*! unique name */
237 const char *name;
Alexander Couzens6a161492020-07-12 13:45:50 +0200238 /*! list entry in nsi */
239 struct llist_head list;
240 /*! list of all VC */
241 struct llist_head nsvc;
242 /*! driver private structure */
243 void *priv;
244 /*! a pointer back to the nsi */
245 struct gprs_ns2_inst *nsi;
246 struct gprs_ns2_vc_driver *driver;
247
Alexander Couzensd923cff2020-12-01 01:03:52 +0100248 bool accept_ipaccess;
249 bool accept_sns;
Alexander Couzens6a161492020-07-12 13:45:50 +0200250
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100251 /*! transfer capability in mbit */
252 int transfer_capability;
253
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100254 /*! MTU of a NS PDU on this bind. */
255 uint16_t mtu;
256
Alexander Couzensaac90162020-11-19 02:44:04 +0100257 /*! which link-layer are we based on? */
258 enum gprs_ns2_ll ll;
259
Alexander Couzens6a161492020-07-12 13:45:50 +0200260 /*! send a msg over a VC */
261 int (*send_vc)(struct gprs_ns2_vc *nsvc, struct msgb *msg);
262
263 /*! free the vc priv data */
264 void (*free_vc)(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200265
Alexander Couzens22f34712020-10-02 02:34:39 +0200266 /*! allow to show information for the vty */
267 void (*dump_vty)(const struct gprs_ns2_vc_bind *bind,
268 struct vty *vty, bool stats);
Harald Welte76346072021-01-31 11:54:02 +0100269
Alexander Couzensc4704762021-02-08 23:13:12 +0100270 /*! the IP-SNS signalling weight when doing dynamic configuration */
271 uint8_t sns_sig_weight;
272 /*! the IP-SNS data weight when doing dynamic configuration */
273 uint8_t sns_data_weight;
274
Harald Welte76346072021-01-31 11:54:02 +0100275 struct osmo_stat_item_group *statg;
Alexander Couzens22f34712020-10-02 02:34:39 +0200276};
Alexander Couzens6a161492020-07-12 13:45:50 +0200277
278struct gprs_ns2_vc_driver {
279 const char *name;
280 void *priv;
281 void (*free_bind)(struct gprs_ns2_vc_bind *driver);
282};
283
Alexander Couzensba5a9922021-01-25 16:03:23 +0100284enum ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
Alexander Couzens6a161492020-07-12 13:45:50 +0200285 struct msgb *msg,
Harald Welte7d0daac2021-03-02 23:08:43 +0100286 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200287 const char *logname,
288 struct msgb **reject,
289 struct gprs_ns2_vc **success);
290
Alexander Couzensffd49d02020-09-24 00:47:17 +0200291int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200292 struct msgb *msg);
293
294struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind,
295 struct gprs_ns2_nse *nse,
Alexander Couzensd923cff2020-12-01 01:03:52 +0100296 bool initiater,
Harald Welte603f4042020-11-29 17:39:19 +0100297 enum gprs_ns2_vc_mode vc_mode,
298 const char *id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200299
Harald Weltec3aa8f92021-01-31 11:41:34 +0100300int ns2_bind_alloc(struct gprs_ns2_inst *nsi, const char *name,
301 struct gprs_ns2_vc_bind **result);
302
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100303struct msgb *ns2_msgb_alloc(void);
Alexander Couzens6a161492020-07-12 13:45:50 +0200304
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100305void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse);
306void ns2_sns_dump_vty(struct vty *vty, const char *prefix, const struct gprs_ns2_nse *nse, bool stats);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200307void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100308 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200309 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200310 enum gprs_ns2_affecting_cause cause);
311void ns2_nse_notify_alive(struct gprs_ns2_vc *nsvc, bool alive);
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100312void ns2_nse_update_mtu(struct gprs_ns2_nse *nse);
Harald Welte06d9bf92021-03-05 10:20:11 +0100313int ns2_nse_set_dialect(struct gprs_ns2_nse *nse, enum gprs_ns2_dialect dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +0200314
315/* message */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100316int ns2_validate(struct gprs_ns2_vc *nsvc,
317 uint8_t pdu_type,
318 struct msgb *msg,
319 struct tlv_parsed *tp,
320 uint8_t *cause);
Alexander Couzens6a161492020-07-12 13:45:50 +0200321
322/* SNS messages */
323int ns2_tx_sns_ack(struct gprs_ns2_vc *nsvc, uint8_t trans_id, uint8_t *cause,
324 const struct gprs_ns_ie_ip4_elem *ip4_elems,
325 unsigned int num_ip4_elems,
326 const struct gprs_ns_ie_ip6_elem *ip6_elems,
327 unsigned int num_ip6_elems);
328int ns2_tx_sns_config(struct gprs_ns2_vc *nsvc, bool end_flag,
329 const struct gprs_ns_ie_ip4_elem *ip4_elems,
330 unsigned int num_ip4_elems,
331 const struct gprs_ns_ie_ip6_elem *ip6_elems,
332 unsigned int num_ip6_elems);
333int ns2_tx_sns_config_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
334int ns2_tx_sns_size(struct gprs_ns2_vc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
335 int ip4_ep_nr, int ip6_ep_nr);
336int ns2_tx_sns_size_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
337
338/* transmit message over a VC */
339int ns2_tx_block(struct gprs_ns2_vc *nsvc, uint8_t cause);
340int ns2_tx_block_ack(struct gprs_ns2_vc *nsvc);
341
342int ns2_tx_reset(struct gprs_ns2_vc *nsvc, uint8_t cause);
343int ns2_tx_reset_ack(struct gprs_ns2_vc *nsvc);
344
345int ns2_tx_unblock(struct gprs_ns2_vc *nsvc);
346int ns2_tx_unblock_ack(struct gprs_ns2_vc *nsvc);
347
348int ns2_tx_alive(struct gprs_ns2_vc *nsvc);
349int ns2_tx_alive_ack(struct gprs_ns2_vc *nsvc);
350
351int ns2_tx_unit_data(struct gprs_ns2_vc *nsvc,
352 uint16_t bvci, uint8_t sducontrol,
353 struct msgb *msg);
354
355int ns2_tx_status(struct gprs_ns2_vc *nsvc, uint8_t cause,
356 uint16_t bvci, struct msgb *orig_msg);
357
358/* driver */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100359struct gprs_ns2_vc *ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind,
360 struct gprs_ns2_nse *nse,
361 const struct osmo_sockaddr *remote);
Alexander Couzense769f522020-12-07 07:37:07 +0100362int ns2_ip_count_bind(struct gprs_ns2_inst *nsi, struct osmo_sockaddr *remote);
363struct gprs_ns2_vc_bind *ns2_ip_get_bind_by_index(struct gprs_ns2_inst *nsi,
364 struct osmo_sockaddr *remote,
365 int index);
Alexander Couzens6a161492020-07-12 13:45:50 +0200366
367/* sns */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100368int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200369struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
370 const char *id);
Harald Welte4f127462021-03-02 20:49:10 +0100371struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100372void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc);
373void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive);
Alexander Couzensc4704762021-02-08 23:13:12 +0100374void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200375
376/* vc */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100377struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200378 const char *id, bool initiate);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100379int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc);
380int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc);
381int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
382int ns2_vc_is_alive(struct gprs_ns2_vc *nsvc);
383int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100384int ns2_vc_block(struct gprs_ns2_vc *nsvc);
385int ns2_vc_unblock(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200386
Alexander Couzens6a161492020-07-12 13:45:50 +0200387/* nse */
388void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100389enum gprs_ns2_vc_mode ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect);
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100390int ns2_count_transfer_cap(struct gprs_ns2_nse *nse,
391 uint16_t bvci);
Harald Welted164ef82021-03-04 22:29:17 +0100392
393/* vty */
394int ns2_sns_add_sns_default_binds(struct gprs_ns2_nse *nse);