blob: cbaa46851da0f96b6a6c965c9cd750ae94e0c9b6 [file] [log] [blame]
Harald Welte047f3872018-07-01 21:04:45 +02001/* Implementation of 3GPP TS 48.016 NS IP Sub-Network Service */
2/* (C) 2018 by Harald Welte <laforge@gnumonks.org> */
3
4/* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures
5 * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and
6 * associated weights. In theory, the BSS then uses this to establish a full mesh
7 * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports */
8
9#include <errno.h>
10
11#include <netinet/in.h>
12#include <arpa/inet.h>
13
14#include <osmocom/core/fsm.h>
15#include <osmocom/core/msgb.h>
16#include <osmocom/core/signal.h>
17#include <osmocom/core/socket.h>
18#include <osmocom/gsm/tlv.h>
19#include <osmocom/gprs/gprs_msgb.h>
20#include <osmocom/gprs/gprs_ns.h>
21
22#include "common_vty.h"
23#include "gb_internal.h"
24
25#define S(x) (1 << (x))
26
27struct gprs_sns_state {
28 struct gprs_ns_inst *nsi;
29 struct gprs_nsvc *nsvc_hack;
30
31 /* local configuration to send to the remote end */
32 struct gprs_ns_ie_ip4_elem *ip4_local;
33 size_t num_ip4_local;
34
35 /* local configuration about our capabilities in terms of connections to
36 * remote (SGSN) side */
37 size_t num_max_nsvcs;
38 size_t num_max_ip4_remote;
39
40 /* remote configuration as received */
41 struct gprs_ns_ie_ip4_elem *ip4_remote;
42 unsigned int num_ip4_remote;
43
44 /* IP-SNS based Gb doesn't have a NSVCI. However, our existing Gb stack
45 * requires a unique NSVCI per NS-VC. Let's simply allocate them dynamically from
46 * the maximum (65533), counting downwards */
47 uint16_t next_nsvci;
48};
49
50static inline struct gprs_ns_inst *ns_inst_from_fi(struct osmo_fsm_inst *fi)
51{
52 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
53 return gss->nsi;
54}
55
56/* helper function to compute the sum of all (data or signaling) weights */
57static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
58 bool data_weight)
59{
60 unsigned int i;
61 int weight_sum = 0;
62
63 for (i = 0; i < num; i++) {
64 if (data_weight)
65 weight_sum += ip4[i].data_weight;
66 else
67 weight_sum += ip4[i].sig_weight;
68 }
69 return weight_sum;
70}
71#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
72#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
73
74static struct gprs_nsvc *nsvc_by_ip4_elem(struct gprs_ns_inst *nsi,
75 const struct gprs_ns_ie_ip4_elem *ip4)
76{
77 struct sockaddr_in sin;
78 /* copy over. Both data structures use network byte order */
79 sin.sin_addr.s_addr = ip4->ip_addr;
80 sin.sin_port = ip4->udp_port;
81 return gprs_nsvc_by_rem_addr(nsi, &sin);
82}
83
84static struct gprs_nsvc *gprs_nsvc_create_ip4(struct gprs_ns_inst *nsi,
85 const struct gprs_ns_ie_ip4_elem *ip4)
86{
87 struct gprs_sns_state *gss = (struct gprs_sns_state *) nsi->bss_sns_fi->priv;
88 struct gprs_nsvc *nsvc;
89 struct sockaddr_in sin;
90 /* copy over. Both data structures use network byte order */
91 sin.sin_addr.s_addr = ip4->ip_addr;
92 sin.sin_port = ip4->udp_port;
93
94 nsvc = gprs_nsvc_create2(nsi, gss->next_nsvci--, ip4->sig_weight, ip4->data_weight);
95 if (!nsvc)
96 return NULL;
97
98 /* NSEI is the same across all NS-VCs */
99 nsvc->nsei = gss->nsvc_hack->nsei;
100 nsvc->nsvci_is_valid = 0;
101 nsvc->ip.bts_addr = sin;
102
103 return nsvc;
104}
105
106static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
107{
108 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
109 struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
110 unsigned int i;
111
112 for (i = 0; i < gss->num_ip4_remote; i++) {
113 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
114 struct gprs_nsvc *nsvc = nsvc_by_ip4_elem(nsi, ip4);
115 if (!nsvc) {
116 /* create, if it doesn't exist */
117 nsvc = gprs_nsvc_create_ip4(nsi, ip4);
118 if (!nsvc) {
119 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
120 continue;
121 }
122 } else {
123 /* update data / signalling weight */
124 nsvc->data_weight = ip4->data_weight;
125 nsvc->sig_weight = ip4->sig_weight;
126 }
127 LOGPFSML(fi, LOGL_INFO, "NS-VC %s data_weight=%u, sig_weight=%u\n",
128 gprs_ns_ll_str(nsvc), nsvc->data_weight, nsvc->sig_weight);
129 }
130
131 return 0;
132}
133
134/* Add a given remote IPv4 element to gprs_sns_state */
135static int add_remote_ip4_elem(struct gprs_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
136{
137 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
138 return -E2BIG;
139
140 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
141 gss->num_ip4_remote+1);
142 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
143 gss->num_ip4_remote += 1;
144 return 0;
145}
146
147/* Remove a given remote IPv4 element from gprs_sns_state */
148static int remove_remote_ip4_elem(struct gprs_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
149{
150 unsigned int i;
151
152 for (i = 0; i < gss->num_ip4_remote; i++) {
153 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
154 continue;
155 /* all array elements < i remain as they are; all > i are shifted left by one */
156 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
157 gss->num_ip4_remote -= 1;
158 return 0;
159 }
160 return -1;
161}
162
163/* update the weights for specified remote IPv4 */
164static int update_remote_ip4_elem(struct gprs_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
165{
166 unsigned int i;
167
168 for (i = 0; i < gss->num_ip4_remote; i++) {
169 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
170 gss->ip4_remote[i].udp_port != ip4->udp_port)
171 continue;
172 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
173 gss->ip4_remote[i].data_weight = ip4->data_weight;
174 return 0;
175 }
176 return -1;
177}
178
179
180static int do_sns_change_weight(struct osmo_fsm_inst *fi, const struct gprs_ns_ie_ip4_elem *ip4)
181{
182 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
183 struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
184 struct gprs_nsvc *nsvc = nsvc_by_ip4_elem(nsi, ip4);
185
186 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
187 * signalling weights of all the peer IP endpoints configured for this NSE is
188 * equal to zero or if the resulting sum of the data weights of all the peer IP
189 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
190 * SNS-ACK PDU with a cause code of "Invalid weights". */
191
192 update_remote_ip4_elem(gss, ip4);
193
194 if (!nsvc) {
195 LOGPFSML(fi, LOGL_NOTICE, "Couldn't find NS-VC for SNS-CHANGE_WEIGHT\n");
196 return -NS_CAUSE_NSVC_UNKNOWN;
197 }
198
199 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
200 gprs_ns_ll_str(nsvc), nsvc->data_weight, ip4->data_weight,
201 nsvc->sig_weight, ip4->sig_weight);
202
203 nsvc->data_weight = ip4->data_weight;
204 nsvc->sig_weight = ip4->sig_weight;
205
206 return 0;
207}
208
209static int do_sns_delete(struct osmo_fsm_inst *fi, const struct gprs_ns_ie_ip4_elem *ip4)
210{
211 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
212 struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
213 struct gprs_nsvc *nsvc = nsvc_by_ip4_elem(nsi, ip4);
214
215 if (remove_remote_ip4_elem(gss, ip4) < 0)
216 return -NS_CAUSE_UNKN_IP_EP;
217
218 if (!nsvc) {
219 LOGPFSML(fi, LOGL_NOTICE, "Couldn't find NS-VC for SNS-DELETE\n");
220 return -NS_CAUSE_NSVC_UNKNOWN;
221 }
222 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns_ll_str(nsvc));
223 gprs_nsvc_delete(nsvc);
224
225 return 0;
226}
227
228static int do_sns_add(struct osmo_fsm_inst *fi, const struct gprs_ns_ie_ip4_elem *ip4)
229{
230 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
231 struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
232 struct gprs_nsvc *nsvc;
233
234 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
235 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
236 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
237 if (add_remote_ip4_elem(gss, ip4) < 0)
238 return -NS_CAUSE_INVAL_NR_NS_VC;
239
240 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
241 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
242 * unspecified" */
243 nsvc = nsvc_by_ip4_elem(nsi, ip4);
244 if (nsvc)
245 return -NS_CAUSE_PROTO_ERR_UNSPEC;
246
247 nsvc = gprs_nsvc_create_ip4(nsi, ip4);
248 if (!nsvc) {
249 LOGPFSML(fi, LOGL_ERROR, "SNS-ADD: Failed to create NSVC\n");
250 remove_remote_ip4_elem(gss, ip4);
251 return -NS_CAUSE_EQUIP_FAIL;
252 }
253 LOGPFSML(fi, LOGL_INFO, "ADD NS-VC %s data_weight=%u, sig_weight=%u\n",
254 gprs_ns_ll_str(nsvc), nsvc->data_weight, nsvc->sig_weight);
255 /* Start the test procedure for this new NS-VC */
256 gprs_nsvc_start_test(nsvc);
257 return 0;
258}
259
260
261
262/***********************************************************************
263 * BSS-side FSM for IP Sub-Network Service
264 ***********************************************************************/
265
266enum gprs_sns_bss_state {
267 GPRS_SNS_ST_UNCONFIGURED,
268 GPRS_SNS_ST_SIZE, /*!< SNS-SIZE procedure ongoing */
269 GPRS_SNS_ST_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
270 GPRS_SNS_ST_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
271 GPRS_SNS_ST_CONFIGURED,
272};
273
274enum gprs_sns_event {
275 GPRS_SNS_EV_START,
276 GPRS_SNS_EV_SIZE,
277 GPRS_SNS_EV_SIZE_ACK,
278 GPRS_SNS_EV_CONFIG,
279 GPRS_SNS_EV_CONFIG_END, /*!< SNS-CONFIG with end flag received */
280 GPRS_SNS_EV_CONFIG_ACK,
281 GPRS_SNS_EV_ADD,
282 GPRS_SNS_EV_DELETE,
283 GPRS_SNS_EV_CHANGE_WEIGHT,
284};
285
286static const struct value_string gprs_sns_event_names[] = {
287 { GPRS_SNS_EV_START, "START" },
288 { GPRS_SNS_EV_SIZE, "SIZE" },
289 { GPRS_SNS_EV_SIZE_ACK, "SIZE_ACK" },
290 { GPRS_SNS_EV_CONFIG, "CONFIG" },
291 { GPRS_SNS_EV_CONFIG_END, "CONFIG_END" },
292 { GPRS_SNS_EV_CONFIG_ACK, "CONFIG_ACK" },
293 { GPRS_SNS_EV_ADD, "ADD" },
294 { GPRS_SNS_EV_DELETE, "DELETE" },
295 { GPRS_SNS_EV_CHANGE_WEIGHT, "CHANGE_WEIGHT" },
296 { 0, NULL }
297};
298
299static void gprs_sns_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
300{
301 struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
302 switch (event) {
303 case GPRS_SNS_EV_START:
304 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
305 break;
306 default:
307 OSMO_ASSERT(0);
308 }
309}
310
311static void gprs_sns_st_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
312{
313 struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
314 struct tlv_parsed *tp = NULL;
315
316 switch (event) {
317 case GPRS_SNS_EV_SIZE_ACK:
318 tp = data;
319 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
320 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
321 gprs_ns_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
322 /* FIXME: What to do? */
323 } else {
324 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS,
325 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
326 }
327 break;
328 default:
329 OSMO_ASSERT(0);
330 }
331}
332static void gprs_sns_st_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
333{
334 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
335 uint16_t num_max_ip4_remote = gss->num_max_ip4_remote;
336
337 gprs_ns_tx_sns_size(gss->nsvc_hack, true, gss->num_max_nsvcs, &num_max_ip4_remote, NULL);
338}
339
340
341static void gprs_sns_st_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
342{
343 //struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
344 //struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
345 struct tlv_parsed *tp = NULL;
346
347 switch (event) {
348 case GPRS_SNS_EV_CONFIG_ACK:
349 tp = data;
350 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
351 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
352 gprs_ns_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
353 /* FIXME: What to do? */
354 } else {
355 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, 0, 0);
356 }
357 break;
358 default:
359 OSMO_ASSERT(0);
360 }
361}
362static void gprs_sns_st_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
363{
364 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
365 /* Transmit SNS-CONFIG */
366 gprs_ns_tx_sns_config(gss->nsvc_hack, true, gss->ip4_local, gss->num_ip4_local);
367}
368
369static void gprs_sns_st_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
370{
371 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
372 struct tlv_parsed *tp = NULL;
373 struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
374 const struct gprs_ns_ie_ip4_elem *v4_list;
375 unsigned int num_v4;
376 uint8_t cause;
377
378 switch (event) {
379 case GPRS_SNS_EV_CONFIG_END:
380 case GPRS_SNS_EV_CONFIG:
381 tp = data;
382#if 0 /* part of incoming SNS-SIZE (doesn't happen on BSS side */
383 if (TLVP_PRESENT(tp, NS_IE_RESET_FLAG)) {
384 /* reset all existing config */
385 if (gss->ip4_remote)
386 talloc_free(gss->ip4_remote);
387 gss->num_ip4_remote = 0;
388 }
389#endif
390 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
391 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
392 gprs_ns_tx_sns_config_ack(gss->nsvc_hack, &cause);
393 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
394 break;
395 }
396 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
397 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
398 /* realloc to the new size */
399 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
400 struct gprs_ns_ie_ip4_elem,
401 gss->num_ip4_remote+num_v4);
402 /* append the new entries to the end of the list */
403 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
404 gss->num_ip4_remote += num_v4;
405
406 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
407 gss->num_ip4_remote);
408 if (event == GPRS_SNS_EV_CONFIG_END) {
409 /* check if sum of data / sig weights == 0 */
410 if (ip4_weight_sum_data(gss->ip4_remote, gss->num_ip4_remote) == 0 ||
411 ip4_weight_sum_sig(gss->ip4_remote, gss->num_ip4_remote) == 0) {
412 cause = NS_CAUSE_INVAL_WEIGH;
413 gprs_ns_tx_sns_config_ack(gss->nsvc_hack, &cause);
414 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
415 break;
416 }
417 create_missing_nsvcs(fi);
418 gprs_ns_tx_sns_config_ack(gss->nsvc_hack, NULL);
419 /* start the test procedure on ALL NSVCs! */
420 gprs_start_alive_all_nsvcs(nsi);
421 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
422 } else {
423 /* just send CONFIG-ACK */
424 gprs_ns_tx_sns_config_ack(gss->nsvc_hack, NULL);
425 }
426 break;
427 default:
428 OSMO_ASSERT(0);
429 }
430}
431
432static void gprs_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
433{
434 struct gprs_sns_state *gss = (struct gprs_sns_state *) fi->priv;
435 struct tlv_parsed *tp = NULL;
436 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
437 unsigned int num_v4 = 0;
438 uint8_t trans_id;
439 uint8_t cause = 0xff;
440 unsigned int i;
441 int rc;
442
443 switch (event) {
444 case GPRS_SNS_EV_ADD:
445 tp = data;
446 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
447 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
448 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
449 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
450 for (i = 0; i < num_v4; i++) {
451 rc = do_sns_add(fi, &v4_list[i]);
452 if (rc < 0) {
453 unsigned int j;
454 /* rollback/undo to restore previous state */
455 for (j = 0; j < i; j++)
456 do_sns_delete(fi, &v4_list[j]);
457 cause = -rc;
458 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, &cause, NULL, 0);
459 break;
460 }
461 }
462 } else {
463 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
464 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, &cause, NULL, 0);
465 break;
466 }
467 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, NULL, v4_list, num_v4);
468 break;
469 case GPRS_SNS_EV_DELETE:
470 tp = data;
471 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
472 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
473 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
474 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
475 for (i = 0; i < num_v4; i++) {
476 rc = do_sns_delete(fi, &v4_list[i]);
477 if (rc < 0) {
478 cause = -rc;
479 /* continue to delete others */
480 }
481 }
482 if (cause != 0xff) {
483 /* TODO: create list of not-deleted and return it */
484 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, &cause, NULL, 0);
485 break;
486 }
487 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 5)) {
488 /* delete all NS-VCs for given IP address */
489 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
490 struct gprs_ns_ie_ip4_elem *ip4_remote;
491 uint32_t ip_addr = *(uint32_t *)(ie+1);
492 if (ie[0] != 0x01) { /* Address Type != IPv4 */
493 cause = NS_CAUSE_UNKN_IP_ADDR;
494 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, &cause, NULL, 0);
495 break;
496 }
497 /* make a copy as do_sns_delete() will change the array underneath us */
498 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
499 gss->num_ip4_remote*sizeof(v4_list));
500 for (i = 0; i < gss->num_ip4_remote; i++) {
501 if (ip4_remote[i].ip_addr == ip_addr) {
502 rc = do_sns_delete(fi, &ip4_remote[i]);
503 if (rc < 0) {
504 cause = -rc;
505 /* continue to delete others */
506 }
507 }
508 }
509 talloc_free(ip4_remote);
510 if (cause != 0xff) {
511 /* TODO: create list of not-deleted and return it */
512 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, &cause, NULL, 0);
513 break;
514 }
515 } else {
516 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
517 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, &cause, NULL, 0);
518 break;
519 }
520 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, NULL, v4_list, num_v4);
521 break;
522 case GPRS_SNS_EV_CHANGE_WEIGHT:
523 tp = data;
524 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
525 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
526 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
527 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
528 for (i = 0; i < num_v4; i++) {
529 rc = do_sns_change_weight(fi, &v4_list[i]);
530 if (rc < 0) {
531 cause = -rc;
532 /* continue to others */
533 }
534 }
535 if (cause != 0xff) {
536 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, &cause, NULL, 0);
537 break;
538 }
539 } else {
540 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
541 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, &cause, NULL, 0);
542 break;
543 }
544 gprs_ns_tx_sns_ack(gss->nsvc_hack, trans_id, NULL, v4_list, num_v4);
545 break;
546 }
547}
548
549static void gprs_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
550{
551 struct ns_signal_data nssd = {0};
552 osmo_signal_dispatch(SS_L_NS, S_SNS_CONFIGURED, &nssd);
553}
554
555static const struct osmo_fsm_state gprs_sns_bss_states[] = {
556 [GPRS_SNS_ST_UNCONFIGURED] = {
557 .in_event_mask = S(GPRS_SNS_EV_START),
558 .out_state_mask = S(GPRS_SNS_ST_SIZE),
559 .name = "UNCONFIGURED",
560 .action = gprs_sns_st_unconfigured,
561 },
562 [GPRS_SNS_ST_SIZE] = {
563 .in_event_mask = S(GPRS_SNS_EV_SIZE_ACK),
564 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
565 S(GPRS_SNS_ST_SIZE) |
566 S(GPRS_SNS_ST_CONFIG_BSS),
567 .name = "SIZE",
568 .action = gprs_sns_st_size,
569 .onenter = gprs_sns_st_size_onenter,
570 },
571 [GPRS_SNS_ST_CONFIG_BSS] = {
572 .in_event_mask = S(GPRS_SNS_EV_CONFIG_ACK),
573 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
574 S(GPRS_SNS_ST_CONFIG_BSS) |
575 S(GPRS_SNS_ST_CONFIG_SGSN),
576 .name = "CONFIG_BSS",
577 .action = gprs_sns_st_config_bss,
578 .onenter = gprs_sns_st_config_bss_onenter,
579 },
580 [GPRS_SNS_ST_CONFIG_SGSN] = {
581 .in_event_mask = S(GPRS_SNS_EV_CONFIG) |
582 S(GPRS_SNS_EV_CONFIG_END),
583 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
584 S(GPRS_SNS_ST_CONFIG_SGSN) |
585 S(GPRS_SNS_ST_CONFIGURED),
586 .name = "CONFIG_SGSN",
587 .action = gprs_sns_st_config_sgsn,
588 },
589 [GPRS_SNS_ST_CONFIGURED] = {
590 .in_event_mask = S(GPRS_SNS_EV_ADD) |
591 S(GPRS_SNS_EV_DELETE) |
592 S(GPRS_SNS_EV_CHANGE_WEIGHT),
593 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
594 .name = "CONFIGURED",
595 .action = gprs_sns_st_configured,
596 .onenter = gprs_sns_st_configured_onenter,
597 },
598};
599
600static int gprs_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
601{
602 struct gprs_ns_inst *nsi = ns_inst_from_fi(fi);
603
604 switch (fi->T) {
605 case 1:
606 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
607 break;
608 case 2:
609 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2);
610 break;
611 }
612 return 0;
613}
614
615static struct osmo_fsm gprs_sns_bss_fsm = {
616 .name = "GPRS-SNS-BSS",
617 .states = gprs_sns_bss_states,
618 .num_states = ARRAY_SIZE(gprs_sns_bss_states),
619 .allstate_event_mask = 0,
620 .allstate_action = NULL,
621 .cleanup = NULL,
622 .timer_cb = gprs_sns_fsm_bss_timer_cb,
623 /* .log_subsys = DNS, "is not constant" */
624 .event_names = gprs_sns_event_names,
625 .pre_term = NULL,
626};
627
628struct osmo_fsm_inst *gprs_sns_bss_fsm_alloc(void *ctx, struct gprs_nsvc *nsvc,
629 const char *id)
630{
631 struct osmo_fsm_inst *fi;
632 struct gprs_sns_state *gss;
633 struct gprs_ns_ie_ip4_elem *ip4;
634 struct gprs_ns_inst *nsi = nsvc->nsi;
635
636 fi = osmo_fsm_inst_alloc(&gprs_sns_bss_fsm, ctx, NULL, LOGL_DEBUG, id);
637 if (!fi)
638 return fi;
639
640 gss = talloc_zero(fi, struct gprs_sns_state);
641 if (!gss)
642 goto err;
643
644 fi->priv = gss;
645 gss->nsi = nsi;
646 /* FIXME: we shouldn't use 'nsvc' here but only gprs_ns_inst */
647 gss->nsvc_hack = nsvc;
648 gss->next_nsvci = 65533; /* 65534 + 65535 are already used internally */
649
650 /* create IPv4 list from the one IP/port the NS instance has */
651 ip4 = talloc_zero(gss, struct gprs_ns_ie_ip4_elem);
652 if (!ip4)
653 goto err;
654 if (nsi->nsip.local_ip)
655 ip4->ip_addr = htonl(nsi->nsip.local_ip);
656 else {
657 /* unspecified local address. Figure out which address the kernel would use if we
658 * wanted to send a packet to the remote_ip */
659 char local_ip[32];
Harald Welte24e67f82019-03-16 18:06:22 +0100660 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
661 osmo_sock_local_ip(local_ip, inet_ntoa(daddr->sin_addr));
Harald Welte047f3872018-07-01 21:04:45 +0200662 ip4->ip_addr = inet_addr(local_ip);
663 }
664 ip4->udp_port = htons(gss->nsi->nsip.local_port);
665 ip4->sig_weight = 2;
666 ip4->data_weight = 1;
667 gss->ip4_local = ip4;
668 gss->num_ip4_local = 1;
669 gss->num_max_nsvcs = 8;
670 gss->num_max_ip4_remote = 4;
671
672 return fi;
673err:
674 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
675 return NULL;
676}
677
678int gprs_sns_bss_fsm_start(struct gprs_ns_inst *nsi)
679{
680 return osmo_fsm_inst_dispatch(nsi->bss_sns_fi, GPRS_SNS_EV_START, NULL);
681}
682
683/* main entry point for receiving SNS messages from the network */
684int gprs_ns_rx_sns(struct gprs_ns_inst *nsi, struct msgb *msg, struct tlv_parsed *tp)
685{
686 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
687 uint16_t nsei = msgb_nsei(msg);
688 struct osmo_fsm_inst *fi;
689
690 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
691 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
692
693 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
694 fi = nsi->bss_sns_fi;
695
696 switch (nsh->pdu_type) {
697 case SNS_PDUT_SIZE:
698 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_SIZE, tp);
699 break;
700 case SNS_PDUT_SIZE_ACK:
701 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_SIZE_ACK, tp);
702 break;
703 case SNS_PDUT_CONFIG:
704 if (nsh->data[0] & 0x01)
705 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_CONFIG_END, tp);
706 else
707 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_CONFIG, tp);
708 break;
709 case SNS_PDUT_CONFIG_ACK:
710 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_CONFIG_ACK, tp);
711 break;
712 case SNS_PDUT_ADD:
713 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_ADD, tp);
714 break;
715 case SNS_PDUT_DELETE:
716 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_DELETE, tp);
717 break;
718 case SNS_PDUT_CHANGE_WEIGHT:
719 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_CHANGE_WEIGHT, tp);
720 break;
721 case SNS_PDUT_ACK:
722 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx unsupported SNS PDU type %s\n", nsei,
723 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
724 break;
725 default:
726 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
727 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
728 return -EINVAL;
729 }
730
731 return 0;
732}
733
734int gprs_sns_init(void)
735{
736 /* "DNS" is not a constant/#define, but an integer variable set by the client app */
737 gprs_sns_bss_fsm.log_subsys = DNS;
738 return osmo_fsm_register(&gprs_sns_bss_fsm);
739}
740
741#include <osmocom/vty/vty.h>
742#include <osmocom/vty/misc.h>
743
744static void vty_dump_sns_ip4(struct vty *vty, const struct gprs_ns_ie_ip4_elem *ip4)
745{
746 struct in_addr in = { .s_addr = ip4->ip_addr };
747 vty_out(vty, " %s:%u, Signalling Weight: %u, Data Weight: %u%s",
748 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
749}
750
751void gprs_sns_dump_vty(struct vty *vty, const struct gprs_ns_inst *nsi, bool stats)
752{
753 struct gprs_sns_state *gss;
754 unsigned int i;
755
756 if (!nsi->bss_sns_fi)
757 return;
758
759 vty_out_fsm_inst(vty, nsi->bss_sns_fi);
760 gss = (struct gprs_sns_state *) nsi->bss_sns_fi->priv;
761
762 vty_out(vty, "Maximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu%s",
763 gss->num_max_nsvcs, gss->num_max_ip4_remote, VTY_NEWLINE);
764
765 vty_out(vty, "Local IPv4 Endpoints:%s", VTY_NEWLINE);
766 for (i = 0; i < gss->num_ip4_local; i++)
767 vty_dump_sns_ip4(vty, &gss->ip4_local[i]);
768
769 vty_out(vty, "Remote IPv4 Endpoints:%s", VTY_NEWLINE);
770 for (i = 0; i < gss->num_ip4_remote; i++)
771 vty_dump_sns_ip4(vty, &gss->ip4_remote[i]);
772}