gerrit-log
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
June 2022
- 3 participants
- 1374 discussions
Attention is currently required from: neels.
pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/osmo-upf/+/28307 )
Change subject: add libgtpnl dependency
......................................................................
Patch Set 1: Code-Review+1
--
To view, visit https://gerrit.osmocom.org/c/osmo-upf/+/28307
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-upf
Gerrit-Branch: master
Gerrit-Change-Id: I9928be6f62f5a89d98bdac63428f7a046c95c855
Gerrit-Change-Number: 28307
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
Gerrit-Attention: neels <nhofmeyr(a)sysmocom.de>
Gerrit-Comment-Date: Fri, 17 Jun 2022 15:14:27 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
1
0
Attention is currently required from: neels.
pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/osmo-upf/+/28287 )
Change subject: move libosmo-gtlv to libosmo-pfcp.git
......................................................................
Patch Set 2: Code-Review+1
--
To view, visit https://gerrit.osmocom.org/c/osmo-upf/+/28287
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-upf
Gerrit-Branch: master
Gerrit-Change-Id: Id72cdf94da60d4b6d09d0044c74e672c4412c15d
Gerrit-Change-Number: 28287
Gerrit-PatchSet: 2
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
Gerrit-Attention: neels <nhofmeyr(a)sysmocom.de>
Gerrit-Comment-Date: Fri, 17 Jun 2022 15:14:07 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
1
0
pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-hlr/+/28313 )
Change subject: ctrl: Mark function as static
......................................................................
ctrl: Mark function as static
Also move it more towards the end, where it is more expected as usually
done in ctrl/vty code.
Change-Id: Ieb956fb855752cfbe83d11aa0326685229a115e3
---
M include/osmocom/hlr/ctrl.h
M src/ctrl.c
2 files changed, 13 insertions(+), 14 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/13/28313/1
diff --git a/include/osmocom/hlr/ctrl.h b/include/osmocom/hlr/ctrl.h
index 3f9ba3f..6ca9163 100644
--- a/include/osmocom/hlr/ctrl.h
+++ b/include/osmocom/hlr/ctrl.h
@@ -30,5 +30,4 @@
_LAST_CTRL_NODE_HLR
};
-int hlr_ctrl_cmds_install();
struct ctrl_handle *hlr_controlif_setup(struct hlr *hlr);
diff --git a/src/ctrl.c b/src/ctrl.c
index b78be12..37177c5 100644
--- a/src/ctrl.c
+++ b/src/ctrl.c
@@ -351,19 +351,6 @@
return set_subscr_cs_ps_enabled(cmd, data, false);
}
-int hlr_ctrl_cmds_install()
-{
- int rc = 0;
-
- rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info);
- rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_aud);
- rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_all);
- rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_ps_enabled);
- rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_cs_enabled);
-
- return rc;
-}
-
static int hlr_ctrl_node_lookup(void *data, vector vline, int *node_type,
void **node_data, int *i)
{
@@ -389,6 +376,19 @@
return 1;
}
+static int hlr_ctrl_cmds_install()
+{
+ int rc = 0;
+
+ rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info);
+ rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_aud);
+ rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_all);
+ rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_ps_enabled);
+ rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_cs_enabled);
+
+ return rc;
+}
+
struct ctrl_handle *hlr_controlif_setup(struct hlr *hlr)
{
int rc;
--
To view, visit https://gerrit.osmocom.org/c/osmo-hlr/+/28313
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-Change-Id: Ieb956fb855752cfbe83d11aa0326685229a115e3
Gerrit-Change-Number: 28313
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-MessageType: newchange
1
0
17 Jun '22
pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-hlr/+/28314 )
Change subject: tests: Allow specyfing specific ctrl test to run
......................................................................
tests: Allow specyfing specific ctrl test to run
Similar to what's already available for vty tests.
Change-Id: I3b01edfb22197f4d46bd24e15bdcd37ea0382e4a
---
M tests/Makefile.am
1 file changed, 4 insertions(+), 1 deletion(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/14/28314/1
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9270efa..41a9d47 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -69,6 +69,9 @@
CTRL_TEST_DB = hlr_ctrl_test.db
+# Run a specific test with: 'make ctrl-test CTRL_TEST=test_subscriber.ctrl'
+CTRL_TEST ?= *.ctrl
+
# To update the CTRL script from current application behavior,
# pass -u to ctrl_script_runner.py by doing:
# make ctrl-test U=-u
@@ -79,7 +82,7 @@
osmo_verify_transcript_ctrl.py -v \
-p 4259 \
-r "$(top_builddir)/src/osmo-hlr -c $(top_srcdir)/doc/examples/osmo-hlr.cfg -l $(CTRL_TEST_DB)" \
- $(U) $(srcdir)/*.ctrl
+ $(U) $(srcdir)/$(CTRL_TEST)
-rm -f $(CTRL_TEST_DB)
-rm $(CTRL_TEST_DB)-*
--
To view, visit https://gerrit.osmocom.org/c/osmo-hlr/+/28314
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-Change-Id: I3b01edfb22197f4d46bd24e15bdcd37ea0382e4a
Gerrit-Change-Number: 28314
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-MessageType: newchange
1
0
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-upf/+/28311 )
Change subject: add contrib/set_cap_net_admin.sh
......................................................................
add contrib/set_cap_net_admin.sh
Related: SYS#5599
Change-Id: If31c304e2602d3c37d5d8a5a2705417b2fc4686c
---
A contrib/set_cap_net_admin.sh
1 file changed, 4 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-upf refs/changes/11/28311/1
diff --git a/contrib/set_cap_net_admin.sh b/contrib/set_cap_net_admin.sh
new file mode 100755
index 0000000..01e0521
--- /dev/null
+++ b/contrib/set_cap_net_admin.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+set -x
+bin="${1:-$(which osmo-upf)}"
+setcap cap_net_admin+pe "$bin"
--
To view, visit https://gerrit.osmocom.org/c/osmo-upf/+/28311
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-upf
Gerrit-Branch: master
Gerrit-Change-Id: If31c304e2602d3c37d5d8a5a2705417b2fc4686c
Gerrit-Change-Number: 28311
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: newchange
1
0
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-upf/+/28312 )
Change subject: implement GTPv1-U ECHO response
......................................................................
implement GTPv1-U ECHO response
Accept data on the GTPv1-U socket and respond to GTPv1-U ECHO REQUEST
messages.
We should keep a deterministic recovery counter that increases with
every restart. As a quick and dirty way just use the current time at
startup for now, until osmo-upf reaches production maturity.
Related: OS#5599
Change-Id: I135370a7723e2c667ec681f50c21107cde63ea5b
---
M include/osmocom/upf/Makefile.am
M include/osmocom/upf/upf.h
A include/osmocom/upf/upf_gtpu_echo.h
M src/osmo-upf/Makefile.am
M src/osmo-upf/upf.c
M src/osmo-upf/upf_gtp.c
A src/osmo-upf/upf_gtpu_echo.c
7 files changed, 169 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-upf refs/changes/12/28312/1
diff --git a/include/osmocom/upf/Makefile.am b/include/osmocom/upf/Makefile.am
index 333132c..127e03e 100644
--- a/include/osmocom/upf/Makefile.am
+++ b/include/osmocom/upf/Makefile.am
@@ -4,6 +4,7 @@
up_session.h \
upf.h \
upf_gtp.h \
+ upf_gtpu_echo.h \
upf_nft.h \
up_gtp_action.h \
$(NULL)
diff --git a/include/osmocom/upf/upf.h b/include/osmocom/upf/upf.h
index 49217c6..6f4d01c 100644
--- a/include/osmocom/upf/upf.h
+++ b/include/osmocom/upf/upf.h
@@ -84,6 +84,8 @@
struct mnl_socket *nl;
int32_t genl_id;
+
+ uint8_t recovery_count;
} gtp;
/* Tunnel forwarding via linux netfilter */
diff --git a/include/osmocom/upf/upf_gtpu_echo.h b/include/osmocom/upf/upf_gtpu_echo.h
new file mode 100644
index 0000000..2575424
--- /dev/null
+++ b/include/osmocom/upf/upf_gtpu_echo.h
@@ -0,0 +1,4 @@
+/* GTP-U ECHO implementation for osmo-upf */
+#pragma once
+
+int upf_gtpu_echo_setup(struct upf_gtp_dev *dev);
diff --git a/src/osmo-upf/Makefile.am b/src/osmo-upf/Makefile.am
index 1265051..1d43ee8 100644
--- a/src/osmo-upf/Makefile.am
+++ b/src/osmo-upf/Makefile.am
@@ -37,6 +37,7 @@
up_session.c \
upf.c \
upf_gtp.c \
+ upf_gtpu_echo.c \
upf_nft.c \
upf_vty.c \
$(NULL)
diff --git a/src/osmo-upf/upf.c b/src/osmo-upf/upf.c
index 9677147..2faa558 100644
--- a/src/osmo-upf/upf.c
+++ b/src/osmo-upf/upf.c
@@ -53,6 +53,10 @@
.nft = {
.priority = -300,
},
+ .gtp = {
+ /* TODO: recovery count state file; use lower byte of current time, poor person's random. */
+ .recovery_count = time(NULL),
+ },
};
INIT_LLIST_HEAD(&g_upf->gtp.vty_cfg.devs);
diff --git a/src/osmo-upf/upf_gtp.c b/src/osmo-upf/upf_gtp.c
index 84d74d1..4524795 100644
--- a/src/osmo-upf/upf_gtp.c
+++ b/src/osmo-upf/upf_gtp.c
@@ -36,6 +36,7 @@
#include <osmocom/upf/upf.h>
#include <osmocom/upf/upf_gtp.h>
+#include <osmocom/upf/upf_gtpu_echo.h>
#define LOG_GTP_TUN(TUN, LEVEL, FMT, ARGS...) \
LOGP(DGTP, LEVEL, "%s: " FMT, upf_gtp_tun_to_str_c(OTC_SELECT, (TUN)), ##ARGS)
@@ -221,6 +222,8 @@
return -EIO;
}
+ upf_gtpu_echo_setup(dev);
+
return 0;
}
diff --git a/src/osmo-upf/upf_gtpu_echo.c b/src/osmo-upf/upf_gtpu_echo.c
new file mode 100644
index 0000000..24452b7
--- /dev/null
+++ b/src/osmo-upf/upf_gtpu_echo.c
@@ -0,0 +1,154 @@
+/* GTP-U ECHO implementation for osmo-upf */
+
+#include <stdint.h>
+#include <errno.h>
+
+#include <osmocom/core/endian.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/msgb.h>
+
+#include <osmocom/upf/upf.h>
+#include <osmocom/upf/upf_gtp.h>
+
+#define GTP1U_PORT 2152
+
+enum gtp1u_msgt {
+ GTP1U_MSGTYPE_ECHO_REQ = 1,
+ GTP1U_MSGTYPE_ECHO_RSP = 2,
+};
+
+enum gtp1u_iei {
+ GTP1U_IEI_RECOVERY = 14,
+};
+
+/* 3GPP TS 29.281 */
+struct gtp1u_hdr {
+#if OSMO_IS_LITTLE_ENDIAN
+ uint8_t pn:1, /*< N-PDU Number flag */
+ s:1, /*< Sequence number flag */
+ e:1, /*< Extension header flag */
+ spare:1,
+ pt:1, /*< Protocol Type: GTP=1, GTP'=0 */
+ version:3; /*< Version: 1 */
+#elif OSMO_IS_BIG_ENDIAN
+ uint8_t version:3, pt:1, spare:1, e:1, s:1, pn:1;
+#endif
+ uint8_t msg_type;
+ uint16_t length;
+ uint32_t tei; /*< 05 - 08 Tunnel Endpoint ID */
+ union {
+ uint8_t data1[0];
+ struct {
+ uint16_t seq_nr;
+ uint8_t n_pdu_nr;
+ uint8_t next_ext_type;
+ } ext;
+ };
+ uint8_t data2[0];
+} __attribute__((packed));
+
+static int rx_echo_req(struct upf_gtp_dev *dev, const struct osmo_sockaddr *remote, const struct gtp1u_hdr *rx_h)
+{
+ struct msgb *msg;
+ struct gtp1u_hdr *tx_h;
+ int rc;
+
+ if (!rx_h->s) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "rx GTPv1-U ECHO REQ without sequence nr\n");
+ return -1;
+ }
+
+ msg = msgb_alloc_headroom(1024, 128, "GTP-echo-resp");
+ tx_h = (void*)msgb_put(msg, sizeof(*tx_h));
+
+ *tx_h = (struct gtp1u_hdr){
+ /* 3GPP TS 29.281 5.1 defines that the ECHO REQ & RESP shall contain a sequence nr */
+ .s = 1,
+ .pt = 1,
+ .version = 1,
+ .msg_type = GTP1U_MSGTYPE_ECHO_RSP,
+ .ext = {
+ .seq_nr = rx_h->ext.seq_nr,
+ },
+ };
+
+ OSMO_ASSERT(msg->tail == tx_h->data2);
+
+ /* ECHO RESPONSE shall contain a recovery counter */
+ msgb_put_u8(msg, GTP1U_IEI_RECOVERY);
+ msgb_put_u8(msg, g_upf->gtp.recovery_count);
+
+ osmo_store16be(msg->tail - tx_h->data1, &tx_h->length);
+
+ rc = sendto(dev->gtpv1.ofd.fd, msgb_data(msg), msgb_length(msg), 0, &remote->u.sa, sizeof(*remote));
+ if (rc < 0) {
+ int err = errno;
+ LOG_GTP_DEV(dev, LOGL_ERROR, "GTP1-U sendto(len=%d, to=%s): %s\n", msgb_length(msg),
+ osmo_sockaddr_to_str(remote), strerror(err));
+ }
+ return rc;
+}
+
+int upf_gtpu_echo_read_cb(struct osmo_fd *ofd, unsigned int what)
+{
+ struct upf_gtp_dev *dev = ofd->data;
+
+ ssize_t sz;
+ uint8_t buf[4096];
+ struct osmo_sockaddr remote;
+ socklen_t remote_len = sizeof(remote);
+ const struct gtp1u_hdr *h;
+ uint16_t h_length;
+
+ if ((sz = recvfrom(dev->gtpv1.ofd.fd, buf, sizeof(buf), 0, &remote.u.sa, &remote_len)) < 0) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "recvfrom() failed: %s\n", strerror(errno));
+ return -1;
+ }
+ if (sz == 0) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "recvfrom() yields zero bytes\n");
+ return -1;
+ }
+
+ /* A GTPv1-U header of size 8 is valid, but this code expects to handle only ECHO REQUEST messages. These are
+ * required to have a sequence number, hence this check here consciously uses the full sizeof(*h) == 12. */
+ if (sz < sizeof(*h)) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "rx GTP packet smaller than the GTPv1-U header + sequence nr: %zd < %zu\n",
+ sz, sizeof(*h));
+ return -1;
+ }
+
+ h = (const struct gtp1u_hdr *)buf;
+ if (h->version != 1) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "rx GTP v%u: only GTP version 1 supported\n", h->version);
+ return -1;
+ }
+
+ h_length = osmo_load16be(&h->length);
+ if (offsetof(struct gtp1u_hdr, data1) + h_length > sz) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "rx GTP: header + h.length = %zu > received bytes = %zd\n",
+ offsetof(struct gtp1u_hdr, data1) + h_length, sz);
+ return -1;
+ }
+
+ switch (h->msg_type) {
+ case GTP1U_MSGTYPE_ECHO_REQ:
+ return rx_echo_req(dev, &remote, h);
+ default:
+ LOG_GTP_DEV(dev, LOGL_ERROR, "rx: GTPv1-U message type %u not supported\n", h->msg_type);
+ return -1;
+ }
+
+ return 0;
+}
+
+int upf_gtpu_echo_setup(struct upf_gtp_dev *dev)
+{
+ if (dev->gtpv1.ofd.fd == -1) {
+ LOGP(DGTP, LOGL_ERROR, "Cannot setup GTP-U ECHO: GTP-v1 socket not initialized\n");
+ return -EINVAL;
+ }
+
+ dev->gtpv1.ofd.cb = upf_gtpu_echo_read_cb;
+ dev->gtpv1.ofd.data = dev;
+ return osmo_fd_register(&dev->gtpv1.ofd);
+}
--
To view, visit https://gerrit.osmocom.org/c/osmo-upf/+/28312
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-upf
Gerrit-Branch: master
Gerrit-Change-Id: I135370a7723e2c667ec681f50c21107cde63ea5b
Gerrit-Change-Number: 28312
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: newchange
1
0
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-upf/+/28310 )
Change subject: add osmo-pfcp-tool
......................................................................
add osmo-pfcp-tool
Related: SYS#5599
Change-Id: I34a80d43a14c7b68952c7d337d8042d6f28ceae7
---
M configure.ac
A contrib/osmo-pfcp-tool-scripts/assoc_setup.vty
A contrib/osmo-pfcp-tool-scripts/assoc_setup_retrans.vty
A contrib/osmo-pfcp-tool-scripts/encaps_plus_tunmap.vty
A contrib/osmo-pfcp-tool-scripts/endecaps_session_est.vty
A contrib/osmo-pfcp-tool-scripts/heartbeat.vty
A contrib/osmo-pfcp-tool-scripts/osmo-pfcp-tool.cfg
A contrib/osmo-pfcp-tool-scripts/osmo-upf-11.cfg
A contrib/osmo-pfcp-tool-scripts/osmo-upf-12.cfg
A contrib/osmo-pfcp-tool-scripts/session_est_without_assoc.vty
A contrib/osmo-pfcp-tool-scripts/session_mod.vty
A contrib/osmo-pfcp-tool-scripts/tunmap_session_est.vty
M src/Makefile.am
A src/osmo-pfcp-tool/Makefile.am
A src/osmo-pfcp-tool/osmo_pfcp_tool_main.c
A src/osmo-pfcp-tool/pfcp_tool.c
A src/osmo-pfcp-tool/pfcp_tool.h
A src/osmo-pfcp-tool/pfcp_tool_vty.c
18 files changed, 1,624 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-upf refs/changes/10/28310/1
diff --git a/configure.ac b/configure.ac
index fea27c6..3522af0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -205,6 +205,7 @@
include/osmocom/upf/Makefile
src/Makefile
src/osmo-upf/Makefile
+ src/osmo-pfcp-tool/Makefile
tests/Makefile
tests/atlocal
doc/Makefile
diff --git a/contrib/osmo-pfcp-tool-scripts/assoc_setup.vty b/contrib/osmo-pfcp-tool-scripts/assoc_setup.vty
new file mode 100644
index 0000000..9877acb
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/assoc_setup.vty
@@ -0,0 +1,3 @@
+pfcp-peer 127.0.0.1
+ tx assoc-setup-req
+ sleep 1
diff --git a/contrib/osmo-pfcp-tool-scripts/assoc_setup_retrans.vty b/contrib/osmo-pfcp-tool-scripts/assoc_setup_retrans.vty
new file mode 100644
index 0000000..c580abb
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/assoc_setup_retrans.vty
@@ -0,0 +1,8 @@
+pfcp-peer 127.0.0.1
+ tx assoc-setup-req
+ sleep 3
+ retrans req
+ sleep 5
+ retrans req
+ sleep 1
+ retrans req
diff --git a/contrib/osmo-pfcp-tool-scripts/encaps_plus_tunmap.vty b/contrib/osmo-pfcp-tool-scripts/encaps_plus_tunmap.vty
new file mode 100644
index 0000000..e84deee
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/encaps_plus_tunmap.vty
@@ -0,0 +1,30 @@
+# ACCESS HOP CORE
+# session 23 = tunmap session 42 = encaps/decaps
+# GTP 127.0.0.13 127.0.0.12 127.0.0.11
+# TEID l:23 r:123 <---> r:23 l:123 | l:142 r:42 <---> r:142 l:42 | 192.168.100.42
+#
+# Run two UPF, one listening on / sending from 127.0.0.11, the other on 127.0.0.12.
+# (Each has to match on the sender address of incoming GTP packets.)
+
+timer pfcp x23 0
+
+pfcp-peer 127.0.0.11
+ tx assoc-setup-req
+ sleep 1
+ session endecaps 42
+ ue ip 192.168.100.42
+ gtp access ip 127.0.0.12
+ gtp access teid local 42 remote 142
+ tx session-est-req
+ sleep 1
+
+pfcp-peer 127.0.0.12
+ tx assoc-setup-req
+ sleep 1
+ session tunmap 23
+ gtp core ip 127.0.0.11
+ gtp core teid local 142 remote 42
+ gtp access ip 127.0.0.13
+ gtp access teid local 123 remote 23
+ tx session-est-req
+ sleep 1
diff --git a/contrib/osmo-pfcp-tool-scripts/endecaps_session_est.vty b/contrib/osmo-pfcp-tool-scripts/endecaps_session_est.vty
new file mode 100644
index 0000000..9a19d8c
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/endecaps_session_est.vty
@@ -0,0 +1,8 @@
+timer pfcp x23 0
+pfcp-peer 127.0.0.1
+ tx assoc-setup-req
+ sleep 1
+ session endecaps
+ tx session-est-req forw
+ sleep 5
+ tx session-del-req
diff --git a/contrib/osmo-pfcp-tool-scripts/heartbeat.vty b/contrib/osmo-pfcp-tool-scripts/heartbeat.vty
new file mode 100644
index 0000000..5ac4bf0
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/heartbeat.vty
@@ -0,0 +1,10 @@
+pfcp-peer 127.0.0.1
+ tx heartbeat
+ sleep 2
+ tx heartbeat
+ sleep 2
+ tx heartbeat
+ sleep 2
+ tx heartbeat
+ sleep 2
+ tx heartbeat
diff --git a/contrib/osmo-pfcp-tool-scripts/osmo-pfcp-tool.cfg b/contrib/osmo-pfcp-tool-scripts/osmo-pfcp-tool.cfg
new file mode 100644
index 0000000..a6d4635
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/osmo-pfcp-tool.cfg
@@ -0,0 +1,5 @@
+log stderr
+ logging level set-all info
+
+local-addr 127.0.0.2
+listen
diff --git a/contrib/osmo-pfcp-tool-scripts/osmo-upf-11.cfg b/contrib/osmo-pfcp-tool-scripts/osmo-upf-11.cfg
new file mode 100644
index 0000000..0184d46
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/osmo-upf-11.cfg
@@ -0,0 +1,27 @@
+log stderr
+ logging filter all 1
+ logging color 1
+ logging print level 1
+ logging print category 1
+ logging print category-hex 0
+ logging print file basename last
+ logging print extended-timestamp 1
+ logging level set-all notice
+ logging level set-all info
+ logging level session debug
+ logging level nft debug
+ logging level gtp debug
+#logging level set-all debug
+
+line vty
+ bind 127.0.0.11
+ctrl
+ bind 127.0.0.11
+
+timer pfcp x24 5000
+pfcp
+ local-addr 127.0.0.11
+gtp
+ dev create apn11 127.0.0.11
+nft
+ table-name osmo-upf-11
diff --git a/contrib/osmo-pfcp-tool-scripts/osmo-upf-12.cfg b/contrib/osmo-pfcp-tool-scripts/osmo-upf-12.cfg
new file mode 100644
index 0000000..1fa620d
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/osmo-upf-12.cfg
@@ -0,0 +1,27 @@
+log stderr
+ logging filter all 1
+ logging color 1
+ logging print level 1
+ logging print category 1
+ logging print category-hex 0
+ logging print file basename last
+ logging print extended-timestamp 1
+ logging level set-all notice
+ logging level set-all info
+ logging level session debug
+ logging level nft debug
+ logging level gtp debug
+#logging level set-all debug
+
+line vty
+ bind 127.0.0.12
+ctrl
+ bind 127.0.0.12
+
+timer pfcp x24 5000
+pfcp
+ local-addr 127.0.0.12
+gtp
+ dev create apn12 127.0.0.12
+nft
+ table-name osmo-upf-12
diff --git a/contrib/osmo-pfcp-tool-scripts/session_est_without_assoc.vty b/contrib/osmo-pfcp-tool-scripts/session_est_without_assoc.vty
new file mode 100644
index 0000000..2472292
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/session_est_without_assoc.vty
@@ -0,0 +1,4 @@
+timer pfcp x23 0
+pfcp-peer 127.0.0.1
+ session endecaps
+ tx session-est-req
diff --git a/contrib/osmo-pfcp-tool-scripts/session_mod.vty b/contrib/osmo-pfcp-tool-scripts/session_mod.vty
new file mode 100644
index 0000000..7645c05
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/session_mod.vty
@@ -0,0 +1,14 @@
+timer pfcp x23 0
+pfcp-peer 127.0.0.1
+ tx assoc-setup-req
+ sleep 1
+ session
+ tx session-est-req drop
+ sleep 3
+ tx session-mod-req forw
+ sleep 5
+ tx session-mod-req drop
+ sleep 3
+ tx session-mod-req forw
+ sleep 3
+ tx session-del-req
diff --git a/contrib/osmo-pfcp-tool-scripts/tunmap_session_est.vty b/contrib/osmo-pfcp-tool-scripts/tunmap_session_est.vty
new file mode 100644
index 0000000..b01b088
--- /dev/null
+++ b/contrib/osmo-pfcp-tool-scripts/tunmap_session_est.vty
@@ -0,0 +1,8 @@
+timer pfcp x23 0
+pfcp-peer 127.0.0.1
+ tx assoc-setup-req
+ sleep 1
+ session tunmap
+ tx session-est-req
+ sleep 5
+ tx session-del-req
diff --git a/src/Makefile.am b/src/Makefile.am
index a8ba763..08ceed7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,3 +1,4 @@
SUBDIRS = \
osmo-upf \
+ osmo-pfcp-tool \
$(NULL)
diff --git a/src/osmo-pfcp-tool/Makefile.am b/src/osmo-pfcp-tool/Makefile.am
new file mode 100644
index 0000000..6c23d9f
--- /dev/null
+++ b/src/osmo-pfcp-tool/Makefile.am
@@ -0,0 +1,39 @@
+AM_CPPFLAGS = \
+ $(all_includes) \
+ -I$(top_srcdir)/include \
+ -I$(top_builddir)/include \
+ -I$(top_builddir) \
+ $(NULL)
+
+AM_CFLAGS = \
+ -Wall \
+ $(LIBOSMOCORE_CFLAGS) \
+ $(LIBOSMOVTY_CFLAGS) \
+ $(LIBOSMOCTRL_CFLAGS) \
+ $(LIBOSMOGTLV_CFLAGS) \
+ $(LIBOSMOPFCP_CFLAGS) \
+ $(COVERAGE_CFLAGS) \
+ $(NULL)
+
+AM_LDFLAGS = \
+ $(LIBOSMOCORE_LIBS) \
+ $(LIBOSMOVTY_LIBS) \
+ $(LIBOSMOCTRL_LIBS) \
+ $(LIBOSMOGTLV_LIBS) \
+ $(LIBOSMOPFCP_LIBS) \
+ $(COVERAGE_LDFLAGS) \
+ $(NULL)
+
+noinst_HEADERS = \
+ pfcp_tool.h \
+ $(NULL)
+
+bin_PROGRAMS = \
+ osmo-pfcp-tool \
+ $(NULL)
+
+osmo_pfcp_tool_SOURCES = \
+ osmo_pfcp_tool_main.c \
+ pfcp_tool.c \
+ pfcp_tool_vty.c \
+ $(NULL)
diff --git a/src/osmo-pfcp-tool/osmo_pfcp_tool_main.c b/src/osmo-pfcp-tool/osmo_pfcp_tool_main.c
new file mode 100644
index 0000000..9a4de1c
--- /dev/null
+++ b/src/osmo-pfcp-tool/osmo_pfcp_tool_main.c
@@ -0,0 +1,372 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/core/application.h>
+#include <osmocom/core/signal.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/fsm.h>
+#include <osmocom/core/stats.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/vty/logging.h>
+#include <osmocom/vty/command.h>
+#include <osmocom/vty/misc.h>
+#include <osmocom/vty/cpu_sched_vty.h>
+#include <osmocom/vty/telnet_interface.h>
+#include <osmocom/vty/ports.h>
+#include <osmocom/vty/tdef_vty.h>
+#include <osmocom/ctrl/control_if.h>
+#include <osmocom/ctrl/control_vty.h>
+#include <osmocom/ctrl/ports.h>
+#include <osmocom/core/sockaddr_str.h>
+
+#include <osmocom/pfcp/pfcp_endpoint.h>
+
+#include "pfcp_tool.h"
+
+#define _GNU_SOURCE
+#include <getopt.h>
+
+/* build switches from the configure script */
+#include "config.h"
+
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+
+extern void *tall_vty_ctx;
+
+void *tall_pfcp_tool_ctx = NULL;
+static int quit = 0;
+
+static struct {
+ const char *config_file;
+ int daemonize;
+ enum vty_ref_gen_mode vty_ref_gen_mode;
+ const char *command_file;
+} pfcp_tool_cmdline_config = {
+ .config_file = "osmo-pfcp-tool.cfg",
+ .vty_ref_gen_mode = VTY_REF_GEN_MODE_DEFAULT,
+};
+
+static void print_usage()
+{
+ printf("Usage: osmo-pfcp-tool [command-file.vty]\n telnet localhost %d\n", OSMO_VTY_PORT_PFCP_TOOL);
+}
+
+static void print_help()
+{
+ const struct value_string *vty_ref_gen_mode_name;
+
+ printf("Some useful options:\n");
+ printf(" -h --help This text.\n");
+ printf(" -D --daemonize Fork the process into a background daemon.\n");
+ printf(" -c --config-file filename The config file to use, for logging etc.\n");
+ printf(" -V --version Print the version of OsmoMSC.\n");
+
+ printf("\nVTY reference generation:\n");
+ printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n");
+ printf(" --vty-ref-mode MODE Mode for --vty-ref-xml:\n");
+ /* List all VTY ref gen modes */
+ for (vty_ref_gen_mode_name = vty_ref_gen_mode_names; vty_ref_gen_mode_name->str; vty_ref_gen_mode_name++)
+ printf(" %s: %s\n",
+ vty_ref_gen_mode_name->str,
+ get_value_string(vty_ref_gen_mode_desc, vty_ref_gen_mode_name->value));
+}
+
+static void handle_long_options(const char *prog_name, const int long_option)
+{
+ switch (long_option) {
+ case 1:
+ pfcp_tool_cmdline_config.vty_ref_gen_mode = get_string_value(vty_ref_gen_mode_names, optarg);
+ if (pfcp_tool_cmdline_config.vty_ref_gen_mode < 0) {
+ fprintf(stderr, "%s: Unknown VTY reference generation mode: '%s'\n", prog_name, optarg);
+ exit(2);
+ }
+ break;
+ case 2:
+ fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
+ get_value_string(vty_ref_gen_mode_names, pfcp_tool_cmdline_config.vty_ref_gen_mode),
+ get_value_string(vty_ref_gen_mode_desc, pfcp_tool_cmdline_config.vty_ref_gen_mode));
+ vty_dump_xml_ref_mode(stdout, pfcp_tool_cmdline_config.vty_ref_gen_mode);
+ exit(0);
+ default:
+ fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
+ exit(2);
+ }
+}
+
+static void handle_options(int argc, char **argv)
+{
+ while (1) {
+ int option_index = 0, c;
+ static int long_option = 0;
+ static struct option long_options[] = {
+ {"help", 0, 0, 'h'},
+ {"daemonize", 0, 0, 'D'},
+ {"config-file", 1, 0, 'c'},
+ {"version", 0, 0, 'V' },
+ {"vty-ref-mode", 1, &long_option, 1},
+ {"vty-ref-xml", 0, &long_option, 2},
+ {0, 0, 0, 0}
+ };
+
+ c = getopt_long(argc, argv, "hDc:V", long_options, &option_index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'h':
+ print_usage();
+ print_help();
+ exit(0);
+ case 0:
+ handle_long_options(argv[0], long_option);
+ break;
+ case 'D':
+ pfcp_tool_cmdline_config.daemonize = 1;
+ break;
+ case 'c':
+ pfcp_tool_cmdline_config.config_file = optarg;
+ break;
+ case 'V':
+ print_version(1);
+ exit(0);
+ break;
+ default:
+ /* catch unknown options *as well as* missing arguments. */
+ fprintf(stderr, "%s: Error in command line options. Exiting.\n", argv[0]);
+ exit(-1);
+ }
+ }
+
+ if (argc > optind) {
+ pfcp_tool_cmdline_config.command_file = argv[optind];
+ optind++;
+ }
+
+ if (argc > optind) {
+ fprintf(stderr, "%s: Unsupported positional arguments on command line\n", argv[optind]);
+ exit(2);
+ }
+}
+
+static void signal_handler(int signum)
+{
+ fprintf(stdout, "signal %u received\n", signum);
+
+ switch (signum) {
+ case SIGINT:
+ case SIGTERM:
+ LOGP(DLGLOBAL, LOGL_NOTICE, "Terminating due to signal %d\n", signum);
+ quit++;
+ break;
+ case SIGABRT:
+ osmo_generate_backtrace();
+ /* in case of abort, we want to obtain a talloc report and
+ * then run default SIGABRT handler, who will generate coredump
+ * and abort the process. abort() should do this for us after we
+ * return, but program wouldn't exit if an external SIGABRT is
+ * received.
+ */
+ talloc_report(tall_vty_ctx, stderr);
+ talloc_report_full(tall_pfcp_tool_ctx, stderr);
+ signal(SIGABRT, SIG_DFL);
+ raise(SIGABRT);
+ break;
+ case SIGUSR1:
+ talloc_report(tall_vty_ctx, stderr);
+ talloc_report_full(tall_pfcp_tool_ctx, stderr);
+ break;
+ case SIGUSR2:
+ talloc_report_full(tall_vty_ctx, stderr);
+ break;
+ default:
+ break;
+ }
+}
+
+static const char * const osmo_pfcp_tool_copyright =
+ "OsmoPFCPTool - Osmocom Packet Forwarding Control Protocol tool for testing\r\n"
+ "Copyright (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>\r\n"
+ "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
+ "This is free software: you are free to change and redistribute it.\r\n"
+ "There is NO WARRANTY, to the extent permitted by law.\r\n";
+
+static struct vty_app_info pfcp_tool_vty_app_info = {
+ .name = "osmo-pfcp-tool",
+ .version = PACKAGE_VERSION,
+ .copyright = osmo_pfcp_tool_copyright,
+};
+
+static const struct log_info_cat pfcp_tool_default_categories[] = {
+};
+
+const struct log_info log_info = {
+ .cat = pfcp_tool_default_categories,
+ .num_cat = ARRAY_SIZE(pfcp_tool_default_categories),
+};
+
+int pfcp_tool_mainloop()
+{
+ log_reset_context();
+ osmo_select_main_ctx(0);
+
+ /* If the user hits Ctrl-C the third time, just terminate immediately. */
+ if (quit >= 3)
+ return 1;
+
+ /* Has SIGTERM been received (and not yet been handled)? */
+ if (quit && !osmo_select_shutdown_requested()) {
+ osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
+
+ /* Request write-only mode in osmo_select_main_ctx() */
+ osmo_select_shutdown_request();
+ /* continue the main select loop until all write queues are serviced. */
+ }
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int rc;
+
+ /* Track the use of talloc NULL memory contexts */
+ talloc_enable_null_tracking();
+
+ osmo_fsm_set_dealloc_ctx(OTC_SELECT);
+
+ tall_pfcp_tool_ctx = talloc_named_const(NULL, 1, "osmo-pfcp-tool");
+ pfcp_tool_vty_app_info.tall_ctx = tall_pfcp_tool_ctx;
+
+ msgb_talloc_ctx_init(tall_pfcp_tool_ctx, 0);
+ osmo_signal_talloc_ctx_init(tall_pfcp_tool_ctx);
+
+ osmo_init_logging2(tall_pfcp_tool_ctx, &log_info);
+ log_set_print_category_hex(osmo_stderr_target, 0);
+ log_set_print_category(osmo_stderr_target, 1);
+ log_set_print_level(osmo_stderr_target, 1);
+ log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME);
+ log_set_print_filename_pos(osmo_stderr_target, LOG_FILENAME_POS_LINE_END);
+ log_set_print_extended_timestamp(osmo_stderr_target, 1);
+
+ osmo_fsm_log_timeouts(true);
+ osmo_fsm_log_addr(true);
+
+ osmo_stats_init(tall_pfcp_tool_ctx);
+
+ g_pfcp_tool_alloc(tall_pfcp_tool_ctx);
+
+ /* For --version, vty_init() must be called before handling options */
+ vty_init(&pfcp_tool_vty_app_info);
+
+ ctrl_vty_init(tall_pfcp_tool_ctx);
+ logging_vty_add_cmds();
+ osmo_talloc_vty_add_cmds();
+ osmo_cpu_sched_vty_init(tall_pfcp_tool_ctx);
+ osmo_fsm_vty_add_cmds();
+ osmo_tdef_vty_groups_init(CONFIG_NODE, g_pfcp_tool_tdef_groups);
+
+ pfcp_tool_vty_init_cfg();
+
+ /* Parse options */
+ handle_options(argc, argv);
+
+ if (pfcp_tool_cmdline_config.config_file) {
+ rc = vty_read_config_file(pfcp_tool_cmdline_config.config_file, NULL);
+ if (rc < 0) {
+ LOGP(DLGLOBAL, LOGL_ERROR, "Failed to parse the config file: '%s'\n",
+ pfcp_tool_cmdline_config.config_file);
+ }
+ }
+
+ /* start telnet, after reading config for vty_get_bind_addr() */
+ rc = telnet_init_dynif(tall_pfcp_tool_ctx, &g_pfcp_tool, vty_get_bind_addr(), OSMO_VTY_PORT_PFCP_TOOL);
+ if (rc < 0)
+ return 2;
+
+ /* start control interface, after reading config for ctrl_vty_get_bind_addr() */
+ g_pfcp_tool->ctrl = ctrl_interface_setup_dynip(g_pfcp_tool, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_PFCP_TOOL, NULL);
+ if (!g_pfcp_tool->ctrl) {
+ fprintf(stderr, "Failed to initialize control interface. Exiting.\n");
+ return -1;
+ }
+
+ signal(SIGINT, &signal_handler);
+ signal(SIGTERM, &signal_handler);
+ signal(SIGABRT, &signal_handler);
+ signal(SIGUSR1, &signal_handler);
+ signal(SIGUSR2, &signal_handler);
+ osmo_init_ignore_signals();
+
+ if (pfcp_tool_cmdline_config.daemonize) {
+ rc = osmo_daemonize();
+ if (rc < 0) {
+ perror("Error during daemonize");
+ return 6;
+ }
+ }
+
+ pfcp_tool_mainloop();
+
+ pfcp_tool_vty_init_cmds();
+
+ if (pfcp_tool_cmdline_config.command_file) {
+ printf("Reading '%s'\n", pfcp_tool_cmdline_config.command_file);
+ rc = vty_read_config_file(pfcp_tool_cmdline_config.command_file, NULL);
+ if (rc < 0) {
+ LOGP(DLGLOBAL, LOGL_FATAL, "Failed to parse the command file: '%s'\n",
+ pfcp_tool_cmdline_config.command_file);
+ return 1;
+ }
+ printf("Done reading '%s', waiting for retransmission queue...\n",
+ pfcp_tool_cmdline_config.command_file);
+ do {
+ if (pfcp_tool_mainloop())
+ break;
+ } while (!llist_empty(&g_pfcp_tool->ep->sent_requests)
+ || !llist_empty(&g_pfcp_tool->ep->sent_responses));
+ printf("Done\n");
+ } else {
+ printf("Listening for commands on VTY...\n");
+ do {
+ if (pfcp_tool_mainloop())
+ break;
+ } while (!osmo_select_shutdown_done());
+ }
+
+ osmo_pfcp_endpoint_free(&g_pfcp_tool->ep);
+
+ log_fini();
+
+ /* Report the heap state of talloc contexts, then free, so both ASAN and Valgrind are happy... */
+ //talloc_report_full(tall_pfcp_tool_ctx, stderr);
+ talloc_free(tall_pfcp_tool_ctx);
+
+ //talloc_report_full(tall_vty_ctx, stderr);
+ talloc_free(tall_vty_ctx);
+
+ //talloc_report_full(NULL, stderr);
+ talloc_disable_null_tracking();
+ return 0;
+}
diff --git a/src/osmo-pfcp-tool/pfcp_tool.c b/src/osmo-pfcp-tool/pfcp_tool.c
new file mode 100644
index 0000000..aebb92c
--- /dev/null
+++ b/src/osmo-pfcp-tool/pfcp_tool.c
@@ -0,0 +1,176 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/talloc.h>
+
+#include <osmocom/pfcp/pfcp_endpoint.h>
+
+#include "pfcp_tool.h"
+
+struct g_pfcp_tool *g_pfcp_tool = NULL;
+
+struct osmo_tdef_group g_pfcp_tool_tdef_groups[] = {
+ { .name = "pfcp", .tdefs = osmo_pfcp_tdefs, .desc = "PFCP" },
+ {}
+};
+
+void g_pfcp_tool_alloc(void *ctx)
+{
+ OSMO_ASSERT(g_pfcp_tool == NULL);
+ g_pfcp_tool = talloc_zero(ctx, struct g_pfcp_tool);
+
+ *g_pfcp_tool = (struct g_pfcp_tool){
+ .vty_cfg = {
+ .local_ip = talloc_strdup(g_pfcp_tool, "0.0.0.0"),
+ .local_port = OSMO_PFCP_PORT,
+ },
+ };
+
+ INIT_LLIST_HEAD(&g_pfcp_tool->peers);
+}
+
+struct pfcp_tool_peer *pfcp_tool_peer_find(const struct osmo_sockaddr *remote_addr)
+{
+ struct pfcp_tool_peer *peer;
+ llist_for_each_entry(peer, &g_pfcp_tool->peers, entry) {
+ if (osmo_sockaddr_cmp(&peer->remote_addr, remote_addr) == 0)
+ return peer;
+ }
+ return NULL;
+}
+
+struct pfcp_tool_peer *pfcp_tool_peer_find_or_create(const struct osmo_sockaddr *remote_addr)
+{
+ struct pfcp_tool_peer *peer = pfcp_tool_peer_find(remote_addr);
+ if (peer)
+ return peer;
+
+ peer = talloc_zero(g_pfcp_tool, struct pfcp_tool_peer);
+ peer->remote_addr = *remote_addr;
+ peer->next_seid_state = 0x1234567;
+ INIT_LLIST_HEAD(&peer->sessions);
+ llist_add(&peer->entry, &g_pfcp_tool->peers);
+ return peer;
+}
+
+struct pfcp_tool_session *pfcp_tool_session_find(struct pfcp_tool_peer *peer, uint64_t cp_seid)
+{
+ struct pfcp_tool_session *session;
+ llist_for_each_entry(session, &peer->sessions, entry) {
+ if (session->cp_seid == cp_seid)
+ return session;
+ }
+ return NULL;
+}
+
+struct pfcp_tool_session *pfcp_tool_session_find_or_create(struct pfcp_tool_peer *peer, uint64_t cp_seid,
+ enum up_gtp_action_kind gtp_action)
+{
+ struct pfcp_tool_session *session = pfcp_tool_session_find(peer, cp_seid);
+ if (session)
+ return session;
+
+ session = talloc(peer, struct pfcp_tool_session);
+ *session = (struct pfcp_tool_session){
+ .peer = peer,
+ .cp_seid = cp_seid,
+ .gtp_action = gtp_action,
+ };
+ llist_add(&session->entry, &peer->sessions);
+ return session;
+}
+
+static void rx_assoc_setup_resp(struct osmo_pfcp_endpoint *ep, struct osmo_pfcp_msg *m)
+{
+ if (m->ies.assoc_setup_resp.up_function_features_present)
+ OSMO_LOG_PFCP_MSG(m, LOGL_NOTICE, "Associated. UP Peer features: %s\n",
+ osmo_pfcp_bits_to_str_c(OTC_SELECT,
+ m->ies.assoc_setup_resp.up_function_features.bits,
+ osmo_pfcp_up_feature_strs));
+
+ if (m->ies.assoc_setup_resp.cp_function_features_present)
+ OSMO_LOG_PFCP_MSG(m, LOGL_NOTICE, "Associated. CP Peer features: %s\n",
+ osmo_pfcp_bits_to_str_c(OTC_SELECT,
+ m->ies.assoc_setup_resp.cp_function_features.bits,
+ osmo_pfcp_cp_feature_strs));
+}
+
+static void rx_session_est_resp(struct osmo_pfcp_endpoint *ep, struct osmo_pfcp_msg *m)
+{
+ struct pfcp_tool_peer *peer;
+ struct pfcp_tool_session *session;
+ enum osmo_pfcp_cause *cause = osmo_pfcp_msg_cause(m);
+ if (!cause) {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Session Establishment Response should contain a Cause\n");
+ return;
+ }
+ if (*cause != OSMO_PFCP_CAUSE_REQUEST_ACCEPTED) {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Peer responds that Session Establishment failed\n");
+ return;
+ }
+ if (!m->h.seid_present) {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Session Establishment Response should contain a SEID\n");
+ return;
+ }
+ if (!m->ies.session_est_resp.up_f_seid_present) {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Session Establishment Response without UP F-SEID\n");
+ return;
+ }
+ peer = pfcp_tool_peer_find(&m->remote_addr);
+ if (!peer)
+ return;
+ session = pfcp_tool_session_find(peer, m->h.seid);
+ if (!session)
+ return;
+ session->up_f_seid = m->ies.session_est_resp.up_f_seid;
+}
+
+void pfcp_tool_rx_msg(struct osmo_pfcp_endpoint *ep, struct osmo_pfcp_msg *m, struct osmo_pfcp_msg *req)
+{
+ switch (m->h.message_type) {
+ case OSMO_PFCP_MSGT_ASSOC_SETUP_RESP:
+ rx_assoc_setup_resp(ep, m);
+ break;
+ case OSMO_PFCP_MSGT_SESSION_EST_RESP:
+ rx_session_est_resp(ep, m);
+ break;
+ default: break;
+ }
+}
+
+int peer_tx(struct pfcp_tool_peer *peer, struct osmo_pfcp_msg *m)
+{
+ int rc;
+ rc = osmo_pfcp_endpoint_tx(g_pfcp_tool->ep, m);
+ if (m->is_response)
+ peer->last_resp = *m;
+ else
+ peer->last_req = *m;
+ return rc;
+}
+
+uint64_t peer_new_seid(struct pfcp_tool_peer *peer)
+{
+ return peer->next_seid_state++;
+}
diff --git a/src/osmo-pfcp-tool/pfcp_tool.h b/src/osmo-pfcp-tool/pfcp_tool.h
new file mode 100644
index 0000000..9f04efd
--- /dev/null
+++ b/src/osmo-pfcp-tool/pfcp_tool.h
@@ -0,0 +1,106 @@
+/* Global definitions for osmo-pfcp-tool */
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/tdef.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/sockaddr_str.h>
+
+#include <osmocom/pfcp/pfcp_msg.h>
+
+#include <osmocom/upf/up_gtp_action.h>
+
+struct osmo_tdef;
+struct ctrl_handle;
+
+extern struct osmo_tdef g_pfcp_tool_tdefs[];
+extern struct osmo_tdef_group g_pfcp_tool_tdef_groups[];
+
+struct pfcp_tool_peer {
+ struct llist_head entry;
+
+ struct osmo_sockaddr remote_addr;
+ struct osmo_pfcp_msg last_req;
+ struct osmo_pfcp_msg last_resp;
+
+ uint64_t next_seid_state;
+
+ struct llist_head sessions;
+};
+
+struct pfcp_tool_teid_pair {
+ uint32_t local;
+ uint32_t remote;
+};
+
+struct pfcp_tool_session {
+ struct llist_head entry;
+
+ enum up_gtp_action_kind gtp_action;
+
+ struct pfcp_tool_peer *peer;
+ uint64_t cp_seid;
+ struct osmo_pfcp_ie_f_seid up_f_seid;
+
+ struct {
+ struct pfcp_tool_teid_pair teid;
+ struct osmo_sockaddr_str gtp_ip;
+ } access;
+
+ struct {
+ struct pfcp_tool_teid_pair teid;
+ struct osmo_sockaddr_str gtp_ip;
+ struct osmo_sockaddr_str ue_addr;
+ } core;
+};
+
+struct g_pfcp_tool {
+ struct ctrl_handle *ctrl;
+
+ struct {
+ char *local_ip;
+ uint16_t local_port;
+ } vty_cfg;
+
+ struct osmo_pfcp_endpoint *ep;
+ struct llist_head peers;
+};
+
+extern struct g_pfcp_tool *g_pfcp_tool;
+
+void g_pfcp_tool_alloc(void *ctx);
+void pfcp_tool_vty_init_cfg();
+void pfcp_tool_vty_init_cmds();
+
+int pfcp_tool_mainloop();
+
+struct pfcp_tool_peer *pfcp_tool_peer_find_or_create(const struct osmo_sockaddr *remote_addr);
+struct pfcp_tool_session *pfcp_tool_session_find_or_create(struct pfcp_tool_peer *peer, uint64_t cp_seid,
+ enum up_gtp_action_kind kind);
+void pfcp_tool_rx_msg(struct osmo_pfcp_endpoint *ep, struct osmo_pfcp_msg *m, struct osmo_pfcp_msg *req);
+
+int peer_tx(struct pfcp_tool_peer *peer, struct osmo_pfcp_msg *m);
+uint64_t peer_new_seid(struct pfcp_tool_peer *peer);
diff --git a/src/osmo-pfcp-tool/pfcp_tool_vty.c b/src/osmo-pfcp-tool/pfcp_tool_vty.c
new file mode 100644
index 0000000..988484e
--- /dev/null
+++ b/src/osmo-pfcp-tool/pfcp_tool_vty.c
@@ -0,0 +1,785 @@
+/* osmo-pfcp-tool interface to quagga VTY */
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <osmocom/core/sockaddr_str.h>
+#include <osmocom/core/socket.h>
+
+#include <osmocom/pfcp/pfcp_endpoint.h>
+#include <osmocom/pfcp/pfcp_msg.h>
+
+#include <osmocom/vty/vty.h>
+#include <osmocom/vty/command.h>
+
+#include "pfcp_tool.h"
+
+enum pfcp_tool_vty_node {
+ PEER_NODE = _LAST_OSMOVTY_NODE + 1,
+ SESSION_NODE,
+};
+
+DEFUN(c_local_addr, c_local_addr_cmd,
+ "local-addr IP_ADDR",
+ "Set the local IP address to bind on for PFCP; see also 'listen'\n"
+ "IP address\n")
+{
+ if (g_pfcp_tool->ep != NULL) {
+ vty_out(vty, "Already listening on %s%s",
+ osmo_sockaddr_to_str_c(OTC_SELECT, &g_pfcp_tool->ep->cfg.local_addr),
+ VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ osmo_talloc_replace_string(g_pfcp_tool, &g_pfcp_tool->vty_cfg.local_ip, argv[0]);
+
+ return CMD_SUCCESS;
+}
+
+DEFUN(c_listen, c_listen_cmd,
+ "listen",
+ "Bind local PFCP port and listen; see also 'local-addr'\n")
+{
+ struct osmo_sockaddr_str local_addr;
+ int rc;
+
+ OSMO_ASSERT(g_pfcp_tool);
+ if (g_pfcp_tool->ep != NULL) {
+ vty_out(vty, "Already listening on %s%s",
+ osmo_sockaddr_to_str_c(OTC_SELECT, &g_pfcp_tool->ep->cfg.local_addr),
+ VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ g_pfcp_tool->ep = osmo_pfcp_endpoint_create(g_pfcp_tool, g_pfcp_tool);
+ if (!g_pfcp_tool->ep) {
+ vty_out(vty, "Failed to allocate PFCP endpoint.%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ g_pfcp_tool->ep->rx_msg = pfcp_tool_rx_msg;
+ g_pfcp_tool->ep->seq_nr_state = rand();
+
+ /* Translate address string from VTY config to osmo_sockaddr: first read into osmo_sockaddr_str, then write to
+ * osmo_sockaddr. */
+ osmo_sockaddr_str_from_str(&local_addr, g_pfcp_tool->vty_cfg.local_ip,
+ g_pfcp_tool->vty_cfg.local_port);
+ osmo_sockaddr_str_to_sockaddr(&local_addr, &g_pfcp_tool->ep->cfg.local_addr.u.sas);
+
+ /* Store this address as the local PFCP Node Id */
+ osmo_pfcp_ie_node_id_from_osmo_sockaddr(&g_pfcp_tool->ep->cfg.local_node_id, &g_pfcp_tool->ep->cfg.local_addr);
+
+ rc = osmo_pfcp_endpoint_bind(g_pfcp_tool->ep);
+ if (rc) {
+ vty_out(vty, "Failed to bind PFCP endpoint on %s: %s%s\n",
+ osmo_sockaddr_to_str_c(OTC_SELECT, &g_pfcp_tool->ep->cfg.local_addr), strerror(rc),
+ VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+DEFUN(c_sleep, c_sleep_cmd,
+ "sleep <0-999999> [<0-999>]",
+ "Let some time pass\n"
+ "Seconds to wait\n")
+{
+ int secs = atoi(argv[0]);
+ int msecs = 0;
+ struct osmo_timer_list t = {};
+ if (argc > 1)
+ msecs = atoi(argv[1]);
+
+ vty_out(vty, "zzZ %d.%03ds...%s", secs, msecs, VTY_NEWLINE);
+ vty_flush(vty);
+
+ osmo_timer_setup(&t, NULL, NULL);
+ osmo_timer_schedule(&t, secs, msecs * 1000);
+
+ /* Still operate the message pump while waiting for time to pass */
+ while (t.active && !osmo_select_shutdown_done()) {
+ if (pfcp_tool_mainloop())
+ break;
+ }
+
+ osmo_timer_del(&t);
+ vty_out(vty, "...zzZ %d.%03ds%s", secs, msecs, VTY_NEWLINE);
+ vty_flush(vty);
+ return CMD_SUCCESS;
+}
+
+static struct cmd_node peer_node = {
+ PEER_NODE,
+ "%s(peer)# ",
+ 1,
+};
+
+DEFUN(peer, peer_cmd,
+ "pfcp-peer REMOTE_ADDR",
+ "Enter the 'peer' node for the given remote address\n"
+ "Remote PFCP peer's IP address\n")
+{
+ struct pfcp_tool_peer *peer;
+ struct osmo_sockaddr_str remote_addr_str;
+ struct osmo_sockaddr remote_addr;
+
+ osmo_sockaddr_str_from_str(&remote_addr_str, argv[0], OSMO_PFCP_PORT);
+ osmo_sockaddr_str_to_sockaddr(&remote_addr_str, (struct sockaddr_storage*)&remote_addr);
+
+ peer = pfcp_tool_peer_find_or_create(&remote_addr);
+
+ vty->index = peer;
+ vty->node = PEER_NODE;
+
+ return CMD_SUCCESS;
+}
+
+#define TX_STR "Send a PFCP message to a peer\n"
+
+DEFUN(peer_tx_heartbeat, peer_tx_heartbeat_cmd,
+ "tx heartbeat",
+ TX_STR "Send a Heartbeat Request\n")
+{
+ struct pfcp_tool_peer *peer = vty->index;
+ int rc;
+
+ if (!g_pfcp_tool->ep) {
+ vty_out(vty, "Endpoint not configured%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ vty_out(vty, "Tx Heartbeat Request to %s%s",
+ osmo_sockaddr_to_str_c(OTC_SELECT, &peer->remote_addr), VTY_NEWLINE);
+
+ rc = osmo_pfcp_endpoint_tx_heartbeat_req(g_pfcp_tool->ep, &peer->remote_addr);
+ if (rc) {
+ vty_out(vty, "Failed to transmit: %s%s", strerror(-rc), VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+DEFUN(peer_tx_assoc_setup_req, peer_tx_assoc_setup_req_cmd,
+ "tx assoc-setup-req",
+ TX_STR "Send an Association Setup Request\n")
+{
+ struct pfcp_tool_peer *peer = vty->index;
+ int rc;
+ struct osmo_pfcp_msg *m;
+
+ if (!g_pfcp_tool->ep) {
+ vty_out(vty, "Endpoint not configured%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ m = osmo_pfcp_msg_alloc_tx(OTC_SELECT, &peer->remote_addr, &g_pfcp_tool->ep->cfg.local_node_id, NULL,
+ OSMO_PFCP_MSGT_ASSOC_SETUP_REQ);
+ m->ies.assoc_setup_req.recovery_time_stamp = g_pfcp_tool->ep->recovery_time_stamp;
+
+ m->ies.assoc_setup_req.cp_function_features_present = true;
+ osmo_pfcp_bits_set(m->ies.assoc_setup_req.cp_function_features.bits, OSMO_PFCP_CP_FEAT_BUNDL, true);
+
+ rc = peer_tx(peer, m);
+ if (rc) {
+ vty_out(vty, "Failed to transmit: %s%s", strerror(-rc), VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+DEFUN(peer_retrans_req, peer_retrans_req_cmd,
+ "retrans (req|resp)",
+ "Retransmit the last sent message\n" "Retransmit the last sent PFCP Request\n"
+ "Retransmit the last sent PFCP Response\n")
+{
+ struct pfcp_tool_peer *peer = vty->index;
+ int rc;
+ struct osmo_pfcp_msg *m;
+
+ if (!g_pfcp_tool->ep) {
+ vty_out(vty, "Endpoint not configured%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ m = osmo_pfcp_msg_alloc_tx(OTC_SELECT, &peer->remote_addr, &g_pfcp_tool->ep->cfg.local_node_id, NULL, 0);
+ if (strcmp(argv[0], "req") == 0)
+ *m = peer->last_req;
+ else
+ *m = peer->last_resp;
+
+ OSMO_LOG_PFCP_MSG(m, LOGL_DEBUG, "retrans %s\n", argv[0]);
+
+ rc = osmo_pfcp_endpoint_tx_data(g_pfcp_tool->ep, m);
+ if (rc) {
+ vty_out(vty, "Failed to transmit: %s%s", strerror(-rc), VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+static struct cmd_node session_node = {
+ SESSION_NODE,
+ "%s(session)# ",
+ 1,
+};
+
+DEFUN(session, session_cmd,
+ "session [(endecaps|tunmap)] [<0-18446744073709551615>]",
+ "Enter the 'session' node for the given SEID\n"
+ "Set up GTP tunnel encapsulation/decapsulation (default)\n"
+ "Set up GTP tunnel mapping\n"
+ "local Session Endpoint ID\n")
+{
+ struct pfcp_tool_peer *peer = vty->index;
+ struct pfcp_tool_session *session;
+ enum up_gtp_action_kind gtp_action = UP_GTP_U_ENDECAPS;
+
+ if (argc > 0 && !strcmp(argv[0], "tunmap"))
+ gtp_action = UP_GTP_U_TUNMAP;
+
+ if (argc > 1)
+ session = pfcp_tool_session_find_or_create(peer, atoll(argv[1]), gtp_action);
+ else
+ session = pfcp_tool_session_find_or_create(peer, peer_new_seid(peer), gtp_action);
+
+ vty->index = session;
+ vty->node = SESSION_NODE;
+
+ return CMD_SUCCESS;
+}
+
+DEFUN(s_ue, s_ue_cmd,
+ "ue ip A.B.C.D",
+ "Setup the UE as it appears towards the Core network in plain IP traffic\n"
+ "IP address assigned to the UE\n")
+{
+ struct pfcp_tool_session *session = vty->index;
+ if (osmo_sockaddr_str_from_str2(&session->core.ue_addr, argv[0])) {
+ vty_out(vty, "Error setting UE IP address%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+DEFUN(s_teid, s_teid_cmd,
+ "gtp (access|core) teid local <0-4294967295> remote <0-4294967295>",
+ "Setup TEID used in GTP\n"
+ "Set the TEIDs towards the ACCESS network (towards the radio network and the actual UE)\n"
+ "Set the TEIDs towards the CORE network (towards the internet)\n"
+ "Local TEID, which the UPF expects to see in incoming GTP packets\n"
+ "Local TEID, when 0 tell the UPF to choose (PFCP: FAR F-TEID: CHOOSE=1)\n"
+ "Remote TEID, which the UPF sends out in GTP packets\n"
+ "Remote TEID, which the GTP peer has assigned for itself\n")
+{
+ struct pfcp_tool_session *session = vty->index;
+ struct pfcp_tool_teid_pair *dst;
+ if (!strcmp(argv[0], "access"))
+ dst = &session->access.teid;
+ else
+ dst = &session->core.teid;
+ *dst = (struct pfcp_tool_teid_pair){
+ .local = atoi(argv[1]),
+ .remote = atoi(argv[2]),
+ };
+ return CMD_SUCCESS;
+}
+
+DEFUN(s_gtp, s_gtp_cmd,
+ "gtp (access|core) ip A.B.C.D",
+ "Setup GTP peer\n"
+ "Set the GTP peer towards the ACCESS network (towards the radio network and the actual UE)\n"
+ "Set the GTP peer towards the CORE network (towards the internet)\n"
+ "Set the GTP peer IP address, where to send GTP packets to / receive GTP packets from\n"
+ "GTP peer IP address\n")
+{
+ struct pfcp_tool_session *session = vty->index;
+ struct osmo_sockaddr_str *dst;
+ if (!strcmp(argv[0], "access"))
+ dst = &session->access.gtp_ip;
+ else
+ dst = &session->core.gtp_ip;
+ if (osmo_sockaddr_str_from_str2(dst, argv[1])) {
+ vty_out(vty, "Error setting GTP IP address%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+int session_endecaps_tx_est_req(struct vty *vty, const char **argv, int argc)
+{
+ struct pfcp_tool_session *session = vty->index;
+ struct pfcp_tool_peer *peer = session->peer;
+ int rc;
+ struct osmo_pfcp_msg *m;
+ struct osmo_pfcp_ie_f_teid f_teid_access_local;
+ struct osmo_pfcp_ie_outer_header_creation ohc_access;
+ struct osmo_pfcp_ie_apply_action aa = {};
+ struct osmo_sockaddr ue_addr;
+ struct osmo_pfcp_ie_f_seid cp_f_seid;
+
+ if (!g_pfcp_tool->ep) {
+ vty_out(vty, "Endpoint not configured%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ if (argc > 0 && !strcmp("drop", argv[0]))
+ osmo_pfcp_bits_set(aa.bits, OSMO_PFCP_APPLY_ACTION_DROP, true);
+ else
+ osmo_pfcp_bits_set(aa.bits, OSMO_PFCP_APPLY_ACTION_FORW, true);
+
+ if (osmo_sockaddr_str_to_sockaddr(&session->core.ue_addr, &ue_addr.u.sas)) {
+ vty_out(vty, "Error in UE IP%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ if (session->access.teid.local == 0) {
+ f_teid_access_local = (struct osmo_pfcp_ie_f_teid){
+ .choose_flag = true,
+ .choose = {
+ .ipv4_addr = true,
+ },
+ };
+ } else {
+ f_teid_access_local = (struct osmo_pfcp_ie_f_teid){
+ .fixed = {
+ .teid = session->access.teid.local,
+ .ip_addr = {
+ .v4_present = true,
+ .v4 = g_pfcp_tool->ep->cfg.local_addr,
+ },
+ },
+ };
+ if (osmo_sockaddr_str_to_sockaddr(&session->access.gtp_ip, &f_teid_access_local.fixed.ip_addr.v4.u.sas)) {
+ vty_out(vty, "Error in GTP IP towards Access%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ }
+
+ ohc_access = (struct osmo_pfcp_ie_outer_header_creation){
+ .teid_present = true,
+ .teid = session->access.teid.remote,
+ .ip_addr.v4_present = true,
+ };
+ osmo_pfcp_bits_set(ohc_access.desc_bits, OSMO_PFCP_OUTER_HEADER_CREATION_GTP_U_UDP_IPV4, true);
+ if (osmo_sockaddr_str_to_sockaddr(&session->access.gtp_ip, &ohc_access.ip_addr.v4.u.sas)) {
+ vty_out(vty, "Error in GTP IP towards Access%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ cp_f_seid = (struct osmo_pfcp_ie_f_seid){
+ .seid = session->cp_seid,
+ };
+ osmo_pfcp_ip_addrs_set(&cp_f_seid.ip_addr, &g_pfcp_tool->ep->cfg.local_addr);
+
+ m = osmo_pfcp_msg_alloc_tx(OTC_SELECT, &peer->remote_addr, &g_pfcp_tool->ep->cfg.local_node_id, NULL,
+ OSMO_PFCP_MSGT_SESSION_EST_REQ);
+ m->h.seid_present = true;
+ /* the UPF has yet to assign a SEID for itself, no matter what SEID we (the CPF) use for this session */
+ m->h.seid = 0;
+ /* GTP encapsulation decapsulation: remove header from ACCESS to CORE, add header from CORE towards ACCESS */
+ m->ies.session_est_req = (struct osmo_pfcp_msg_session_est_req){
+ .node_id = m->ies.session_est_req.node_id,
+ .cp_f_seid_present = true,
+ .cp_f_seid = cp_f_seid,
+ .create_pdr_count = 2,
+ .create_pdr = {
+ {
+ .pdr_id = 1,
+ .precedence = 255,
+ .pdi = {
+ .source_iface = OSMO_PFCP_SOURCE_IFACE_CORE,
+ .ue_ip_address_present = true,
+ .ue_ip_address = {
+ .ip_is_destination = true,
+ .ip_addr = {
+ .v4_present = true,
+ .v4 = ue_addr,
+ },
+ },
+ },
+ .far_id_present = true,
+ .far_id = 1,
+ },
+ {
+ .pdr_id = 2,
+ .precedence = 255,
+ .pdi = {
+ .source_iface = OSMO_PFCP_SOURCE_IFACE_ACCESS,
+ .local_f_teid_present = true,
+ .local_f_teid = f_teid_access_local,
+ },
+ .outer_header_removal_present = true,
+ .outer_header_removal = {
+ .desc = OSMO_PFCP_OUTER_HEADER_REMOVAL_GTP_U_UDP_IPV4,
+ },
+ .far_id_present = true,
+ .far_id = 2,
+ },
+ },
+ .create_far_count = 2,
+ .create_far = {
+ {
+ .far_id = 1,
+ .forw_params_present = true,
+ .forw_params = {
+ .destination_iface = OSMO_PFCP_DEST_IFACE_ACCESS,
+ .outer_header_creation_present = true,
+ .outer_header_creation = ohc_access,
+ },
+ .apply_action = aa,
+ },
+ {
+ .far_id = 2,
+ .forw_params_present = true,
+ .forw_params = {
+ .destination_iface = OSMO_PFCP_DEST_IFACE_CORE,
+ },
+ .apply_action = aa,
+ },
+ },
+ };
+
+ rc = peer_tx(peer, m);
+ if (rc) {
+ vty_out(vty, "Failed to transmit: %s%s", strerror(-rc), VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+int session_tunmap_tx_est_req(struct vty *vty, const char **argv, int argc)
+{
+ struct pfcp_tool_session *session = vty->index;
+ struct pfcp_tool_peer *peer = session->peer;
+ int rc;
+ struct osmo_pfcp_msg *m;
+
+ struct osmo_pfcp_ie_f_seid cp_f_seid;
+
+ struct osmo_pfcp_ie_f_teid f_teid_access_local;
+ struct osmo_pfcp_ie_outer_header_creation ohc_access;
+
+ struct osmo_pfcp_ie_f_teid f_teid_core_local;
+ struct osmo_pfcp_ie_outer_header_creation ohc_core;
+
+ struct osmo_pfcp_ie_apply_action aa = {};
+
+ if (!g_pfcp_tool->ep) {
+ vty_out(vty, "Endpoint not configured%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ if (argc > 0 && !strcmp("drop", argv[0]))
+ osmo_pfcp_bits_set(aa.bits, OSMO_PFCP_APPLY_ACTION_DROP, true);
+ else
+ osmo_pfcp_bits_set(aa.bits, OSMO_PFCP_APPLY_ACTION_FORW, true);
+
+ if (session->access.teid.local == 0) {
+ f_teid_access_local = (struct osmo_pfcp_ie_f_teid){
+ .choose_flag = true,
+ .choose = {
+ .ipv4_addr = true,
+ },
+ };
+ } else {
+ f_teid_access_local = (struct osmo_pfcp_ie_f_teid){
+ .fixed = {
+ .teid = session->access.teid.local,
+ .ip_addr = {
+ .v4_present = true,
+ .v4 = g_pfcp_tool->ep->cfg.local_addr,
+ },
+ },
+ };
+ if (osmo_sockaddr_str_to_sockaddr(&session->access.gtp_ip, &f_teid_access_local.fixed.ip_addr.v4.u.sas)) {
+ vty_out(vty, "Error in GTP IP towards Access%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ }
+
+ ohc_access = (struct osmo_pfcp_ie_outer_header_creation){
+ .teid_present = true,
+ .teid = session->access.teid.remote,
+ .ip_addr.v4_present = true,
+ };
+ osmo_pfcp_bits_set(ohc_access.desc_bits, OSMO_PFCP_OUTER_HEADER_CREATION_GTP_U_UDP_IPV4, true);
+ if (osmo_sockaddr_str_to_sockaddr(&session->access.gtp_ip, &ohc_access.ip_addr.v4.u.sas)) {
+ vty_out(vty, "Error in GTP IP towards Access%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ if (session->core.teid.local == 0) {
+ f_teid_core_local = (struct osmo_pfcp_ie_f_teid){
+ .choose_flag = true,
+ .choose = {
+ .ipv4_addr = true,
+ },
+ };
+ } else {
+ f_teid_core_local = (struct osmo_pfcp_ie_f_teid){
+ .fixed = {
+ .teid = session->core.teid.local,
+ .ip_addr = {
+ .v4_present = true,
+ .v4 = g_pfcp_tool->ep->cfg.local_addr,
+ },
+ },
+ };
+ if (osmo_sockaddr_str_to_sockaddr(&session->core.gtp_ip, &f_teid_core_local.fixed.ip_addr.v4.u.sas)) {
+ vty_out(vty, "Error in GTP IP towards Core%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ }
+ ohc_core = (struct osmo_pfcp_ie_outer_header_creation){
+ .teid_present = true,
+ .teid = session->core.teid.remote,
+ .ip_addr.v4_present = true,
+ };
+ osmo_pfcp_bits_set(ohc_core.desc_bits, OSMO_PFCP_OUTER_HEADER_CREATION_GTP_U_UDP_IPV4, true);
+ if (osmo_sockaddr_str_to_sockaddr(&session->core.gtp_ip, &ohc_core.ip_addr.v4.u.sas)) {
+ vty_out(vty, "Error in GTP IP towards Core%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ cp_f_seid = (struct osmo_pfcp_ie_f_seid){
+ .seid = session->cp_seid,
+ };
+ osmo_pfcp_ip_addrs_set(&cp_f_seid.ip_addr, &g_pfcp_tool->ep->cfg.local_addr);
+
+ m = osmo_pfcp_msg_alloc_tx(OTC_SELECT, &peer->remote_addr, &g_pfcp_tool->ep->cfg.local_node_id, NULL,
+ OSMO_PFCP_MSGT_SESSION_EST_REQ);
+ m->h.seid_present = true;
+ m->h.seid = 0;
+ /* GTP tunmap: remove header from both directions, and add header in both directions */
+ m->ies.session_est_req = (struct osmo_pfcp_msg_session_est_req){
+ .node_id = m->ies.session_est_req.node_id,
+ .cp_f_seid_present = true,
+ .cp_f_seid = cp_f_seid,
+ .create_pdr_count = 2,
+ .create_pdr = {
+ {
+ .pdr_id = 1,
+ .precedence = 255,
+ .pdi = {
+ .source_iface = OSMO_PFCP_SOURCE_IFACE_CORE,
+ .local_f_teid_present = true,
+ .local_f_teid = f_teid_core_local,
+ },
+ .outer_header_removal_present = true,
+ .outer_header_removal = {
+ .desc = OSMO_PFCP_OUTER_HEADER_REMOVAL_GTP_U_UDP_IPV4,
+ },
+ .far_id_present = true,
+ .far_id = 1,
+ },
+ {
+ .pdr_id = 2,
+ .precedence = 255,
+ .pdi = {
+ .source_iface = OSMO_PFCP_SOURCE_IFACE_ACCESS,
+ .local_f_teid_present = true,
+ .local_f_teid = f_teid_access_local,
+ },
+ .outer_header_removal_present = true,
+ .outer_header_removal = {
+ .desc = OSMO_PFCP_OUTER_HEADER_REMOVAL_GTP_U_UDP_IPV4,
+ },
+ .far_id_present = true,
+ .far_id = 2,
+ },
+ },
+ .create_far_count = 2,
+ .create_far = {
+ {
+ .far_id = 1,
+ .forw_params_present = true,
+ .forw_params = {
+ .destination_iface = OSMO_PFCP_DEST_IFACE_ACCESS,
+ .outer_header_creation_present = true,
+ .outer_header_creation = ohc_access,
+ },
+ .apply_action = aa,
+ },
+ {
+ .far_id = 2,
+ .forw_params_present = true,
+ .forw_params = {
+ .destination_iface = OSMO_PFCP_DEST_IFACE_CORE,
+ .outer_header_creation_present = true,
+ .outer_header_creation = ohc_core,
+ },
+ .apply_action = aa,
+ },
+ },
+ };
+
+ rc = peer_tx(peer, m);
+ if (rc) {
+ vty_out(vty, "Failed to transmit: %s%s", strerror(-rc), VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+DEFUN(session_tx_est_req, session_tx_est_req_cmd,
+ "tx session-est-req [(forw|drop)]",
+ TX_STR "Send a Session Establishment Request\n"
+ "Set FAR to FORW = 1 (default)\n"
+ "Set FAR to DROP = 1\n")
+{
+ struct pfcp_tool_session *session = vty->index;
+ switch (session->gtp_action) {
+ case UP_GTP_U_ENDECAPS:
+ return session_endecaps_tx_est_req(vty, argv, argc);
+ case UP_GTP_U_TUNMAP:
+ return session_tunmap_tx_est_req(vty, argv, argc);
+ default:
+ vty_out(vty, "unknown gtp action%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+}
+
+DEFUN(session_tx_mod_req, session_tx_mod_req_cmd,
+ "tx session-mod-req far [(forw|drop)]",
+ TX_STR "Send a Session Modification Request\n"
+ "Set FAR to FORW = 1\n"
+ "Set FAR to DROP = 1\n")
+{
+ struct pfcp_tool_session *session = vty->index;
+ struct pfcp_tool_peer *peer = session->peer;
+ int rc;
+ struct osmo_pfcp_msg *m;
+ struct osmo_pfcp_ie_apply_action aa = {};
+ struct osmo_pfcp_ie_f_seid cp_f_seid;
+
+ if (!g_pfcp_tool->ep) {
+ vty_out(vty, "Endpoint not configured%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ if (argc > 0 && !strcmp("drop", argv[0]))
+ osmo_pfcp_bits_set(aa.bits, OSMO_PFCP_APPLY_ACTION_DROP, true);
+ else
+ osmo_pfcp_bits_set(aa.bits, OSMO_PFCP_APPLY_ACTION_FORW, true);
+
+ cp_f_seid = (struct osmo_pfcp_ie_f_seid){
+ .seid = session->cp_seid,
+ };
+ osmo_pfcp_ip_addrs_set(&cp_f_seid.ip_addr, &g_pfcp_tool->ep->cfg.local_addr);
+
+ m = osmo_pfcp_msg_alloc_tx(OTC_SELECT, &peer->remote_addr, &g_pfcp_tool->ep->cfg.local_node_id, NULL,
+ OSMO_PFCP_MSGT_SESSION_MOD_REQ);
+ m->h.seid_present = true;
+ m->h.seid = session->up_f_seid.seid;
+ m->ies.session_mod_req = (struct osmo_pfcp_msg_session_mod_req){
+ .cp_f_seid_present = true,
+ .cp_f_seid = cp_f_seid,
+ .upd_far_count = 2,
+ .upd_far = {
+ {
+ .far_id = 1,
+ .apply_action_present = true,
+ .apply_action = aa,
+ },
+ {
+ .far_id = 2,
+ .apply_action_present = true,
+ .apply_action = aa,
+ },
+ },
+ };
+
+ rc = peer_tx(peer, m);
+ if (rc) {
+ vty_out(vty, "Failed to transmit: %s%s", strerror(-rc), VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+DEFUN(session_tx_del_req, session_tx_del_req_cmd,
+ "tx session-del-req",
+ TX_STR "Send a Session Deletion Request\n")
+{
+ struct pfcp_tool_session *session = vty->index;
+ struct pfcp_tool_peer *peer = session->peer;
+ int rc;
+ struct osmo_pfcp_msg *m;
+
+ if (!g_pfcp_tool->ep) {
+ vty_out(vty, "Endpoint not configured%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ m = osmo_pfcp_msg_alloc_tx(OTC_SELECT, &peer->remote_addr, &g_pfcp_tool->ep->cfg.local_node_id, NULL,
+ OSMO_PFCP_MSGT_SESSION_DEL_REQ);
+ m->h.seid_present = true;
+ m->h.seid = session->up_f_seid.seid;
+
+ rc = peer_tx(peer, m);
+ if (rc) {
+ vty_out(vty, "Failed to transmit: %s%s", strerror(-rc), VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+static void install_ve_and_config(struct cmd_element *cmd)
+{
+ install_element_ve(cmd);
+ install_element(CONFIG_NODE, cmd);
+}
+
+void pfcp_tool_vty_init_cfg()
+{
+ OSMO_ASSERT(g_pfcp_tool != NULL);
+
+ install_ve_and_config(&c_local_addr_cmd);
+ install_ve_and_config(&c_listen_cmd);
+}
+
+void pfcp_tool_vty_init_cmds()
+{
+ OSMO_ASSERT(g_pfcp_tool != NULL);
+
+ install_ve_and_config(&c_sleep_cmd);
+
+ install_ve_and_config(&peer_cmd);
+ install_node(&peer_node, NULL);
+
+ install_element(PEER_NODE, &c_sleep_cmd);
+ install_element(PEER_NODE, &peer_tx_heartbeat_cmd);
+ install_element(PEER_NODE, &peer_tx_assoc_setup_req_cmd);
+ install_element(PEER_NODE, &peer_retrans_req_cmd);
+
+ install_element(PEER_NODE, &session_cmd);
+ install_node(&session_node, NULL);
+ install_element(SESSION_NODE, &c_sleep_cmd);
+ install_element(SESSION_NODE, &session_tx_est_req_cmd);
+ install_element(SESSION_NODE, &session_tx_mod_req_cmd);
+ install_element(SESSION_NODE, &session_tx_del_req_cmd);
+ install_element(SESSION_NODE, &s_ue_cmd);
+ install_element(SESSION_NODE, &s_gtp_cmd);
+ install_element(SESSION_NODE, &s_teid_cmd);
+}
--
To view, visit https://gerrit.osmocom.org/c/osmo-upf/+/28310
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-upf
Gerrit-Branch: master
Gerrit-Change-Id: I34a80d43a14c7b68952c7d337d8042d6f28ceae7
Gerrit-Change-Number: 28310
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: newchange
1
0
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-upf/+/28309 )
Change subject: add nft
......................................................................
add nft
Related: SYS#5599
Change-Id: Ic0d319eb4f98cd51a5999c804c4203ab0bdda650
---
M configure.ac
M include/osmocom/upf/upf.h
M include/osmocom/upf/upf_nft.h
M src/osmo-upf/Makefile.am
M src/osmo-upf/osmo_upf_main.c
M src/osmo-upf/up_gtp_action.c
M src/osmo-upf/up_session.c
M src/osmo-upf/upf.c
A src/osmo-upf/upf_nft.c
M src/osmo-upf/upf_vty.c
10 files changed, 419 insertions(+), 6 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-upf refs/changes/09/28309/1
diff --git a/configure.ac b/configure.ac
index 119aa6c..fea27c6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -45,6 +45,8 @@
PKG_CHECK_MODULES(LIBOSMOGTLV, libosmo-gtlv >= 0.1.0)
PKG_CHECK_MODULES(LIBOSMOPFCP, libosmo-pfcp >= 0.1.0)
PKG_CHECK_MODULES(LIBGTPNL, libgtpnl >= 1.2.0)
+PKG_CHECK_MODULES(LIBNFTNL, libnftnl >= 1.2.1)
+PKG_CHECK_MODULES(LIBNFTABLES, libnftables >= 1.0.2)
dnl checks for header files
AC_HEADER_STDC
diff --git a/include/osmocom/upf/upf.h b/include/osmocom/upf/upf.h
index b8065dd..49217c6 100644
--- a/include/osmocom/upf/upf.h
+++ b/include/osmocom/upf/upf.h
@@ -33,6 +33,7 @@
struct ctrl_handle;
struct upf_gtp_dev;
+struct nft_ctx;
#define UPF_PFCP_LISTEN_DEFAULT "0.0.0.0"
@@ -72,6 +73,8 @@
struct pfcp_vty_cfg vty_cfg;
struct up_endpoint *ep;
} pfcp;
+
+ /* Tunnel encaps decaps via GTP kernel module */
struct {
/* GTP devices as in osmo-upf.cfg */
struct gtp_vty_cfg vty_cfg;
@@ -82,6 +85,14 @@
struct mnl_socket *nl;
int32_t genl_id;
} gtp;
+
+ /* Tunnel forwarding via linux netfilter */
+ struct {
+ struct nft_ctx *nft_ctx;
+ char *table_name;
+ int priority;
+ uint32_t next_id_state;
+ } nft;
};
extern struct g_upf *g_upf;
@@ -91,6 +102,7 @@
DPEER,
DSESSION,
DGTP,
+ DNFT,
};
void g_upf_alloc(void *ctx);
diff --git a/include/osmocom/upf/upf_nft.h b/include/osmocom/upf/upf_nft.h
index 569eadb..113bfd8 100644
--- a/include/osmocom/upf/upf_nft.h
+++ b/include/osmocom/upf/upf_nft.h
@@ -27,6 +27,8 @@
#include <osmocom/core/socket.h>
+#define NFT_CHAIN_NAME_PREFIX_TUNMAP "tunmap"
+
struct upf_nft_tunmap_desc {
struct {
struct osmo_sockaddr gtp_remote_addr;
diff --git a/src/osmo-upf/Makefile.am b/src/osmo-upf/Makefile.am
index d8aa5a0..1265051 100644
--- a/src/osmo-upf/Makefile.am
+++ b/src/osmo-upf/Makefile.am
@@ -13,11 +13,15 @@
$(LIBOSMOGTLV_CFLAGS) \
$(LIBOSMOPFCP_CFLAGS) \
$(LIBGTPNL_CFLAGS) \
+ $(LIBNFTNL_CFLAGS) \
+ $(LIBNFTABLES_CFLAGS) \
$(COVERAGE_CFLAGS) \
$(NULL)
AM_LDFLAGS = \
$(LIBGTPNL_LDFLAGS) \
+ $(LIBNFTNL_LDFLAGS) \
+ $(LIBNFTABLES_LDFLAGS) \
$(COVERAGE_LDFLAGS) \
$(NULL)
@@ -33,6 +37,7 @@
up_session.c \
upf.c \
upf_gtp.c \
+ upf_nft.c \
upf_vty.c \
$(NULL)
@@ -43,5 +48,7 @@
$(LIBOSMOGTLV_LIBS) \
$(LIBOSMOPFCP_LIBS) \
$(LIBGTPNL_LIBS) \
+ $(LIBNFTNL_LIBS) \
+ $(LIBNFTABLES_LIBS) \
$(COVERAGE_LDFLAGS) \
$(NULL)
diff --git a/src/osmo-upf/osmo_upf_main.c b/src/osmo-upf/osmo_upf_main.c
index 302d957..81102dd 100644
--- a/src/osmo-upf/osmo_upf_main.c
+++ b/src/osmo-upf/osmo_upf_main.c
@@ -44,6 +44,7 @@
#include <osmocom/upf/upf.h>
#include <osmocom/upf/up_endpoint.h>
#include <osmocom/upf/upf_gtp.h>
+#include <osmocom/upf/upf_nft.h>
#define _GNU_SOURCE
#include <getopt.h>
@@ -237,6 +238,12 @@
.enabled = 0, .loglevel = LOGL_NOTICE,
.color = OSMO_LOGCOLOR_PURPLE,
},
+ [DNFT] = {
+ .name = "DNFT",
+ .description = "GTP forwarding rules via linux netfilter",
+ .enabled = 0, .loglevel = LOGL_NOTICE,
+ .color = OSMO_LOGCOLOR_PURPLE,
+ },
};
const struct log_info log_info = {
@@ -330,6 +337,9 @@
if (upf_gtp_devs_open())
return -1;
+ if (upf_nft_init())
+ return -1;
+
if (upf_pfcp_listen())
return -1;
@@ -356,6 +366,8 @@
upf_gtp_genl_close();
+ upf_nft_free();
+
/* Report the heap state of talloc contexts, then free, so both ASAN and Valgrind are happy... */
talloc_report_full(tall_upf_ctx, stderr);
talloc_free(tall_upf_ctx);
diff --git a/src/osmo-upf/up_gtp_action.c b/src/osmo-upf/up_gtp_action.c
index 840944e..cf5981e 100644
--- a/src/osmo-upf/up_gtp_action.c
+++ b/src/osmo-upf/up_gtp_action.c
@@ -100,9 +100,36 @@
}
LOG_UP_GTP_ACTION(a, LOGL_NOTICE, "%s GTP tunnel\n", enable ? "Enabled" : "Disabled");
return 0;
+
case UP_GTP_U_TUNMAP:
- LOG_UP_GTP_ACTION(a, LOGL_ERROR, "TEID translation not yet implemented\n");
- return -ENOTSUP;
+ if (enable && a->tunmap.id != 0) {
+ LOG_UP_GTP_ACTION(a, LOGL_ERROR,
+ "Cannot enable: nft GTP tunnel mapping rule has been enabled before"
+ " as " NFT_CHAIN_NAME_PREFIX_TUNMAP "%u\n", a->tunmap.id);
+ return -EALREADY;
+ }
+ if (!enable && a->tunmap.id == 0) {
+ LOG_UP_GTP_ACTION(a, LOGL_ERROR,
+ "Cannot disable: nft GTP tunnel mapping rule has not been enabled"
+ " (no " NFT_CHAIN_NAME_PREFIX_TUNMAP " id)\n");
+ return -ENOENT;
+ }
+ if (enable)
+ rc = upf_nft_tunmap_create(&a->tunmap);
+ else
+ rc = upf_nft_tunmap_delete(&a->tunmap);
+ if (rc) {
+ LOG_UP_GTP_ACTION(a, LOGL_ERROR,
+ "Failed to %s nft GTP tunnel mapping " NFT_CHAIN_NAME_PREFIX_TUNMAP "%u:"
+ " %d %s\n", enable ? "enable" : "disable", a->tunmap.id, rc, strerror(-rc));
+ return rc;
+ }
+ LOG_UP_GTP_ACTION(a, LOGL_NOTICE, "%s nft GTP tunnel mapping " NFT_CHAIN_NAME_PREFIX_TUNMAP "%u\n",
+ enable ? "Enabled" : "Disabled", a->tunmap.id);
+ if (!enable)
+ a->tunmap.id = 0;
+ return 0;
+
default:
LOG_UP_GTP_ACTION(a, LOGL_ERROR, "Invalid action\n");
return -ENOTSUP;
diff --git a/src/osmo-upf/up_session.c b/src/osmo-upf/up_session.c
index d1479b1..00299d3 100644
--- a/src/osmo-upf/up_session.c
+++ b/src/osmo-upf/up_session.c
@@ -1141,7 +1141,107 @@
static void add_gtp_action_forw(void *ctx, struct llist_head *dst, struct pdr *pdr)
{
- /* TODO implement GTP forwarding with TEID translation */
+ struct up_session *session = pdr->session;
+ struct up_gtp_action *a;
+ struct pdr *rpdr;
+ struct far *far;
+ struct osmo_pfcp_ie_forw_params *far_forw;
+ struct far *rfar;
+ struct osmo_pfcp_ie_forw_params *rfar_forw;
+
+ OSMO_ASSERT(pdr->far);
+ OSMO_ASSERT(pdr->reverse_pdr);
+ OSMO_ASSERT(pdr->reverse_pdr->far);
+
+ far = pdr->far;
+ far_forw = &far->desc.forw_params;
+ rpdr = pdr->reverse_pdr;
+ rfar = rpdr->far;
+ rfar_forw = &rfar->desc.forw_params;
+
+ /* To decaps, we need to have a local TEID assigned for which to receive GTP packets. */
+ /* decaps from CORE */
+ if (!pdr->local_f_teid || pdr->local_f_teid->choose_flag) {
+ log_inactive_pdr_set(pdr, "missing local TEID (CORE side)", pdr);
+ return;
+ }
+ /* decaps from ACCESS */
+ if (!rpdr->local_f_teid || rpdr->local_f_teid->choose_flag) {
+ log_inactive_pdr_set(pdr, "missing local TEID (ACCESS side)", pdr);
+ return;
+ }
+
+ /* To encaps, we need to have a remote TEID assigned to send out in GTP packets, and we need to know where to
+ * send GTP to. */
+ /* encaps towards ACCESS */
+ if (!far->desc.forw_params_present) {
+ log_inactive_pdr_set(pdr, "missing FAR Forwarding Parameters", pdr);
+ return;
+ }
+ if (!far_forw->outer_header_creation_present) {
+ log_inactive_pdr_set(pdr, "missing FAR Outer Header Creation", pdr);
+ return;
+ }
+ if (!far_forw->outer_header_creation.teid_present) {
+ log_inactive_pdr_set(pdr, "missing TEID in FAR Outer Header Creation", pdr);
+ return;
+ }
+ if (!far_forw->outer_header_creation.ip_addr.v4_present) {
+ log_inactive_pdr_set(pdr, "missing IPv4 in FAR Outer Header Creation", pdr);
+ return;
+ }
+ /* encaps towards CORE */
+ if (!rfar->desc.forw_params_present) {
+ log_inactive_pdr_set(pdr, "missing FAR Forwarding Parameters", rpdr);
+ return;
+ }
+ if (!rfar_forw->outer_header_creation_present) {
+ log_inactive_pdr_set(pdr, "missing FAR Outer Header Creation", rpdr);
+ return;
+ }
+ if (!rfar_forw->outer_header_creation.teid_present) {
+ log_inactive_pdr_set(pdr, "missing TEID in FAR Outer Header Creation", rpdr);
+ return;
+ }
+ if (!rfar_forw->outer_header_creation.ip_addr.v4_present) {
+ log_inactive_pdr_set(pdr, "missing IPv4 in FAR Outer Header Creation", rpdr);
+ return;
+ }
+
+ pdr->active = true;
+ far->active = true;
+ rpdr->active = true;
+ rfar->active = true;
+ LOGPFSML(session->fi, LOGL_DEBUG, "Active PDR set: %s\n", pdr_to_str_c(OTC_SELECT, pdr));
+ LOGPFSML(session->fi, LOGL_DEBUG, "Active PDR set: + %s\n", pdr_to_str_c(OTC_SELECT, rpdr));
+
+ talloc_free(pdr->inactive_reason);
+ pdr->inactive_reason = NULL;
+ talloc_free(rpdr->inactive_reason);
+ rpdr->inactive_reason = NULL;
+
+ a = talloc(ctx, struct up_gtp_action);
+ OSMO_ASSERT(a);
+ *a = (struct up_gtp_action){
+ .session = session,
+ .pdr_core = pdr->desc.pdr_id,
+ .pdr_access = rpdr->desc.pdr_id,
+ .kind = UP_GTP_U_TUNMAP,
+ .tunmap = {
+ .core = {
+ .local_teid = pdr->local_f_teid->fixed.teid,
+ .remote_teid = rfar_forw->outer_header_creation.teid,
+ .gtp_remote_addr = rfar_forw->outer_header_creation.ip_addr.v4,
+ },
+ .access = {
+ .local_teid = rpdr->local_f_teid->fixed.teid,
+ .remote_teid = far_forw->outer_header_creation.teid,
+ .gtp_remote_addr = far_forw->outer_header_creation.ip_addr.v4,
+ },
+ },
+ };
+
+ llist_add_tail(&a->entry, dst);
}
/* Analyse all PDRs and FARs and find configurations that match either a GTP encaps/decaps or a GTP forward rule. Add to
@@ -1294,11 +1394,12 @@
{
struct llist_head empty;
INIT_LLIST_HEAD(&empty);
+ /* Call setup_gtp_actions() with an empty list, to clean up all active GTP actions */
setup_gtp_actions(session, &empty);
}
-/* Check whether the Packet Detection and Forwarding Action Rules amount to an encaps/decaps of GTP or a GTP forwarding,
- * or none of the two. */
+/* Check whether the Packet Detection and Forwarding Action Rules amount to an encaps/decaps of GTP or a GTP tunnel
+ * mapping, or none of the two. */
static enum osmo_pfcp_cause up_session_setup_gtp(struct up_session *session)
{
enum osmo_pfcp_cause cause;
diff --git a/src/osmo-upf/upf.c b/src/osmo-upf/upf.c
index 234368a..9677147 100644
--- a/src/osmo-upf/upf.c
+++ b/src/osmo-upf/upf.c
@@ -50,6 +50,9 @@
.local_port = OSMO_PFCP_PORT,
},
},
+ .nft = {
+ .priority = -300,
+ },
};
INIT_LLIST_HEAD(&g_upf->gtp.vty_cfg.devs);
diff --git a/src/osmo-upf/upf_nft.c b/src/osmo-upf/upf_nft.c
new file mode 100644
index 0000000..d93dc39
--- /dev/null
+++ b/src/osmo-upf/upf_nft.c
@@ -0,0 +1,207 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <errno.h>
+#include <nftables/libnftables.h>
+
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/logging.h>
+
+#include <osmocom/upf/upf.h>
+#include <osmocom/upf/upf_nft.h>
+
+static char *upf_nft_ruleset_table_create(void *ctx, const char *table_name)
+{
+ return talloc_asprintf(ctx, "add table inet %s\n", table_name);
+}
+
+static int upf_nft_run(const char *ruleset)
+{
+ int rc;
+ if (!g_upf->nft.nft_ctx) {
+ rc = upf_nft_init();
+ if (rc)
+ return rc;
+ }
+
+ rc = nft_run_cmd_from_buffer(g_upf->nft.nft_ctx, ruleset);
+ if (rc < 0) {
+ LOGP(DNFT, LOGL_ERROR, "error running nft ruleset: rc=%d ruleset=%s\n",
+ rc, osmo_quote_str_c(OTC_SELECT, ruleset, -1));
+ return -EIO;
+ }
+ return 0;
+}
+
+int upf_nft_init()
+{
+ int rc;
+ g_upf->nft.nft_ctx = nft_ctx_new(NFT_CTX_DEFAULT);
+ if (!g_upf->nft.nft_ctx) {
+ LOGP(DNFT, LOGL_ERROR, "cannot allocate libnftables nft_ctx\n");
+ return -EIO;
+ }
+
+ if (!g_upf->nft.table_name)
+ g_upf->nft.table_name = talloc_strdup(g_upf, "osmo-upf");
+
+ rc = upf_nft_run(upf_nft_ruleset_table_create(OTC_SELECT, g_upf->nft.table_name));
+ if (rc) {
+ LOGP(DNFT, LOGL_ERROR, "Failed to create nft table %s\n",
+ osmo_quote_str_c(OTC_SELECT, g_upf->nft.table_name, -1));
+ return rc;
+ }
+ LOGP(DNFT, LOGL_NOTICE, "Created nft table %s\n", osmo_quote_str_c(OTC_SELECT, g_upf->nft.table_name, -1));
+ return 0;
+}
+
+int upf_nft_free()
+{
+ if (!g_upf->nft.nft_ctx)
+ return 0;
+ nft_ctx_free(g_upf->nft.nft_ctx);
+ g_upf->nft.nft_ctx = NULL;
+ return 0;
+}
+
+struct upf_nft_args_peer {
+ /* The source IP address in packets received from this peer */
+ const struct osmo_sockaddr *addr;
+ /* The TEID that we send to the peer in GTP packets. */
+ uint32_t teid_remote;
+ /* The TEID that the peer sends to us in GTP packets. */
+ uint32_t teid_local;
+};
+
+struct upf_nft_args {
+ /* global table name */
+ const char *table_name;
+ /* chain name for this specific tunnel mapping */
+ uint32_t chain_id;
+ int priority;
+
+ struct upf_nft_args_peer peer_a;
+ struct upf_nft_args_peer peer_b;
+};
+
+static int tunmap_single_direction(char *buf, size_t buflen,
+ const struct upf_nft_args *args,
+ const struct upf_nft_args_peer *from_peer,
+ const struct upf_nft_args_peer *to_peer)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ OSMO_STRBUF_PRINTF(sb, "add rule inet %s " NFT_CHAIN_NAME_PREFIX_TUNMAP "%u", args->table_name, args->chain_id);
+
+ /* Match only UDP packets */
+ OSMO_STRBUF_PRINTF(sb, " meta l4proto udp");
+
+ /* Match on packets coming in from from_peer */
+ OSMO_STRBUF_PRINTF(sb, " ip saddr ");
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, from_peer->addr);
+
+ /* Match on the TEID in the header */
+ OSMO_STRBUF_PRINTF(sb, " @ih,32,32 0x%08x", from_peer->teid_local);
+
+ /* Change destination address to to_peer */
+ OSMO_STRBUF_PRINTF(sb, " ip daddr set ");
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, to_peer->addr);
+
+ /* Change the TEID in the header to the one to_peer expects */
+ OSMO_STRBUF_PRINTF(sb, " @ih,32,32 set 0x%08x", to_peer->teid_remote);
+
+ OSMO_STRBUF_PRINTF(sb, " counter\n");
+
+ return sb.chars_needed;
+}
+
+static int upf_nft_ruleset_tunmap_create_buf(char *buf, size_t buflen, const struct upf_nft_args *args)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+
+ /* Add a chain for this tunnel mapping */
+ OSMO_STRBUF_PRINTF(sb, "add chain inet %s " NFT_CHAIN_NAME_PREFIX_TUNMAP "%u { type filter hook prerouting priority %d; }\n",
+ args->table_name, args->chain_id, args->priority);
+
+ /* Forwarding from peer_a to peer_b */
+ OSMO_STRBUF_APPEND(sb, tunmap_single_direction, args, &args->peer_a, &args->peer_b);
+ /* And from peer_b to peer_a */
+ OSMO_STRBUF_APPEND(sb, tunmap_single_direction, args, &args->peer_b, &args->peer_a);
+
+ return sb.chars_needed;
+}
+
+static char *upf_nft_ruleset_tunmap_create_c(void *ctx, const struct upf_nft_args *args)
+{
+ OSMO_NAME_C_IMPL(ctx, 512, "ERROR", upf_nft_ruleset_tunmap_create_buf, args)
+}
+
+static int upf_nft_ruleset_tunmap_delete_buf(char *buf, size_t buflen, const struct upf_nft_args *args)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ OSMO_STRBUF_PRINTF(sb, "delete chain inet %s " NFT_CHAIN_NAME_PREFIX_TUNMAP "%u\n",
+ args->table_name, args->chain_id);
+ return sb.chars_needed;
+}
+
+static char *upf_nft_ruleset_tunmap_delete_c(void *ctx, const struct upf_nft_args *args)
+{
+ OSMO_NAME_C_IMPL(ctx, 64, "ERROR", upf_nft_ruleset_tunmap_delete_buf, args)
+}
+
+static void upf_nft_args_from_tunmap_desc(struct upf_nft_args *args, const struct upf_nft_tunmap_desc *tunmap)
+{
+ *args = (struct upf_nft_args){
+ .table_name = g_upf->nft.table_name,
+ .chain_id = tunmap->id,
+ .priority = g_upf->nft.priority,
+ .peer_a = {
+ .addr = &tunmap->access.gtp_remote_addr,
+ .teid_remote = tunmap->access.remote_teid,
+ .teid_local = tunmap->access.local_teid,
+ },
+ .peer_b = {
+ .addr = &tunmap->core.gtp_remote_addr,
+ .teid_remote = tunmap->core.remote_teid,
+ .teid_local = tunmap->core.local_teid,
+ },
+ };
+}
+
+int upf_nft_tunmap_create(struct upf_nft_tunmap_desc *tunmap)
+{
+ struct upf_nft_args args;
+
+ /* Give this tunnel mapping a new id, returned to the caller so that the tunnel mapping can be deleted later */
+ g_upf->nft.next_id_state++;
+ tunmap->id = g_upf->nft.next_id_state;
+
+ upf_nft_args_from_tunmap_desc(&args, tunmap);
+ return upf_nft_run(upf_nft_ruleset_tunmap_create_c(OTC_SELECT, &args));
+}
+
+int upf_nft_tunmap_delete(struct upf_nft_tunmap_desc *tunmap)
+{
+ struct upf_nft_args args;
+ upf_nft_args_from_tunmap_desc(&args, tunmap);
+ return upf_nft_run(upf_nft_ruleset_tunmap_delete_c(OTC_SELECT, &args));
+}
diff --git a/src/osmo-upf/upf_vty.c b/src/osmo-upf/upf_vty.c
index 73d86d1..0e51cd4 100644
--- a/src/osmo-upf/upf_vty.c
+++ b/src/osmo-upf/upf_vty.c
@@ -41,6 +41,7 @@
enum upf_vty_node {
PFCP_NODE = _LAST_OSMOVTY_NODE + 1,
GTP_NODE,
+ NFT_NODE,
};
static struct cmd_node cfg_pfcp_node = {
@@ -85,7 +86,7 @@
DEFUN(cfg_gtp, cfg_gtp_cmd,
"gtp",
- "Enter the GTP configuration node\n")
+ "Enter the 'gtp' node to configure GTP kernel module usage\n")
{
vty->node = GTP_NODE;
return CMD_SUCCESS;
@@ -169,6 +170,40 @@
return CMD_SUCCESS;
}
+static struct cmd_node cfg_nft_node = {
+ NFT_NODE,
+ "%s(config-nft)# ",
+ 1,
+};
+
+DEFUN(cfg_nft, cfg_nft_cmd,
+ "nft",
+ "Enter the 'nft' node to configure nftables usage\n")
+{
+ vty->node = NFT_NODE;
+ return CMD_SUCCESS;
+}
+
+static int config_write_nft(struct vty *vty)
+{
+ vty_out(vty, "nft%s", VTY_NEWLINE);
+
+ if (g_upf->nft.table_name && strcmp(g_upf->nft.table_name, "osmo-upf"))
+ vty_out(vty, " table-name %s%s", g_upf->nft.table_name, VTY_NEWLINE);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_nft_table_name, cfg_nft_table_name_cmd,
+ "table-name TABLE_NAME",
+ "Set the nft inet table name to create and place GTP tunnel forwarding chains in"
+ " (as in 'nft add table inet foo'). If multiple instances of osmo-upf are running on the same system, each"
+ " osmo-upf must have its own table name. Otherwise the names of created forwarding chains will collide.\n"
+ "nft inet table name\n")
+{
+ osmo_talloc_replace_string(g_upf, &g_upf->nft.table_name, argv[0]);
+ return CMD_SUCCESS;
+}
+
DEFUN(show_pdr, show_pdr_cmd,
"show pdr",
SHOW_STR
@@ -282,5 +317,10 @@
install_element(GTP_NODE, &cfg_gtp_dev_create_cmd);
install_element(GTP_NODE, &cfg_gtp_dev_use_cmd);
install_element(GTP_NODE, &cfg_gtp_dev_del_cmd);
+
+ install_node(&cfg_nft_node, config_write_nft);
+ install_element(CONFIG_NODE, &cfg_nft_cmd);
+
+ install_element(NFT_NODE, &cfg_nft_table_name_cmd);
}
--
To view, visit https://gerrit.osmocom.org/c/osmo-upf/+/28309
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-upf
Gerrit-Branch: master
Gerrit-Change-Id: Ic0d319eb4f98cd51a5999c804c4203ab0bdda650
Gerrit-Change-Number: 28309
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: newchange
1
0
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-upf/+/28308 )
Change subject: add osmo-upf
......................................................................
add osmo-upf
Related: SYS#5599
Change-Id: I745bcbde6859004c41ddbfd2558036bf9a2d1de2
---
A doc/examples/osmo-upf/osmo-upf-create-dev.cfg
M doc/examples/osmo-upf/osmo-upf.cfg
M include/osmocom/upf/Makefile.am
A include/osmocom/upf/up_endpoint.h
A include/osmocom/upf/up_gtp_action.h
A include/osmocom/upf/up_peer.h
A include/osmocom/upf/up_session.h
A include/osmocom/upf/up_session_to_gtp.c
M include/osmocom/upf/upf.h
A include/osmocom/upf/upf_gtp.h
A include/osmocom/upf/upf_nft.h
M src/osmo-upf/Makefile.am
M src/osmo-upf/osmo_upf_main.c
A src/osmo-upf/up_endpoint.c
A src/osmo-upf/up_gtp_action.c
A src/osmo-upf/up_peer.c
A src/osmo-upf/up_peer_fsm.c
A src/osmo-upf/up_session.c
M src/osmo-upf/upf.c
A src/osmo-upf/upf_gtp.c
A src/osmo-upf/upf_vty.c
M tests/upf.vty
22 files changed, 4,053 insertions(+), 3 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-upf refs/changes/08/28308/1
diff --git a/doc/examples/osmo-upf/osmo-upf-create-dev.cfg b/doc/examples/osmo-upf/osmo-upf-create-dev.cfg
new file mode 100644
index 0000000..54ca643
--- /dev/null
+++ b/doc/examples/osmo-upf/osmo-upf-create-dev.cfg
@@ -0,0 +1,16 @@
+log stderr
+ logging filter all 1
+ logging color 1
+ logging print level 1
+ logging print category 1
+ logging print category-hex 0
+ logging print file basename last
+ logging print extended-timestamp 1
+ logging level set-all info
+#logging level set-all debug
+
+timer pfcp x24 5000
+pfcp
+ local-addr 127.0.0.1
+gtp
+ dev create apn23
diff --git a/doc/examples/osmo-upf/osmo-upf.cfg b/doc/examples/osmo-upf/osmo-upf.cfg
index 8a2a407..0f241a5 100644
--- a/doc/examples/osmo-upf/osmo-upf.cfg
+++ b/doc/examples/osmo-upf/osmo-upf.cfg
@@ -6,4 +6,10 @@
logging print category-hex 0
logging print file basename last
logging print extended-timestamp 1
+ logging level set-all debug
logging level set-all notice
+ logging level set-all info
+
+timer pfcp x24 5000
+pfcp
+ local-addr 127.0.0.1
diff --git a/include/osmocom/upf/Makefile.am b/include/osmocom/upf/Makefile.am
index 2608e8f..333132c 100644
--- a/include/osmocom/upf/Makefile.am
+++ b/include/osmocom/upf/Makefile.am
@@ -1,3 +1,9 @@
noinst_HEADERS = \
+ up_endpoint.h \
+ up_peer.h \
+ up_session.h \
upf.h \
+ upf_gtp.h \
+ upf_nft.h \
+ up_gtp_action.h \
$(NULL)
diff --git a/include/osmocom/upf/up_endpoint.h b/include/osmocom/upf/up_endpoint.h
new file mode 100644
index 0000000..3bb14ec
--- /dev/null
+++ b/include/osmocom/upf/up_endpoint.h
@@ -0,0 +1,48 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <osmocom/core/linuxlist.h>
+
+struct osmo_pfcp_msg;
+struct osmo_pfcp_endpoint;
+struct osmo_sockaddr;
+
+#define UP_USE_MSG_RX "msg-rx"
+#define UP_USE_MSG_TX "msg-tx"
+
+struct up_endpoint {
+ struct osmo_pfcp_endpoint *pfcp_ep;
+
+ struct llist_head peers;
+
+ uint64_t next_seid_state;
+ uint32_t next_teid_state;
+};
+
+struct up_endpoint *up_endpoint_init(void *ctx, const struct osmo_sockaddr *local_addr);
+void up_endpoint_free(struct up_endpoint **ep);
+
+uint64_t up_endpoint_next_seid(struct up_endpoint *ep);
+uint32_t up_endpoint_next_teid(struct up_endpoint *ep);
diff --git a/include/osmocom/upf/up_gtp_action.h b/include/osmocom/upf/up_gtp_action.h
new file mode 100644
index 0000000..1907ed1
--- /dev/null
+++ b/include/osmocom/upf/up_gtp_action.h
@@ -0,0 +1,73 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/fsm.h>
+
+#include <osmocom/upf/up_session.h>
+#include <osmocom/upf/upf_gtp.h>
+#include <osmocom/upf/upf_nft.h>
+
+#define LOG_UP_GTP_ACTION(A, LEVEL, FMT, ARGS...) \
+ LOGP(DGTP, LEVEL, "%s: " FMT, up_gtp_action_to_str_c(OTC_SELECT, A), ##ARGS)
+
+struct up_session;
+
+enum up_gtp_action_kind {
+ UP_GTP_DROP,
+ UP_GTP_U_ENDECAPS,
+ UP_GTP_U_TUNMAP,
+};
+
+struct up_gtp_action {
+ struct llist_head entry;
+ struct up_session *session;
+
+ uint16_t pdr_core;
+ uint16_t pdr_access;
+
+ enum up_gtp_action_kind kind;
+ union {
+ /* En-/De-capsulate GTP: add/remove a GTP header and forward the GTP payload from/to plain IP. */
+ struct upf_gtp_tun_desc endecaps;
+
+ /* Tunnel-map GTP: translate from one TEID to another and forward */
+ struct upf_nft_tunmap_desc tunmap;
+ };
+
+ /* volatile loop variable to match up wanted and actually present GTP actions */
+ void *handle;
+};
+
+int up_gtp_action_cmp(const struct up_gtp_action *a, const struct up_gtp_action *b);
+
+int up_gtp_action_enable(struct up_gtp_action *a);
+int up_gtp_action_disable(struct up_gtp_action *a);
+
+int up_gtp_action_to_str_buf(char *buf, size_t buflen, const struct up_gtp_action *a);
+char *up_gtp_action_to_str_c(void *ctx, const struct up_gtp_action *a);
diff --git a/include/osmocom/upf/up_peer.h b/include/osmocom/upf/up_peer.h
new file mode 100644
index 0000000..0c168cf
--- /dev/null
+++ b/include/osmocom/upf/up_peer.h
@@ -0,0 +1,78 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/hashtable.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/use_count.h>
+
+#include <osmocom/pfcp/pfcp_msg.h>
+
+enum up_peer_event {
+ UP_PEER_EV_RX_ASSOC_SETUP_REQ,
+ UP_PEER_EV_RX_ASSOC_UPD_REQ,
+ UP_PEER_EV_RX_ASSOC_REL_REQ,
+ UP_PEER_EV_RX_SESSION_EST_REQ,
+ UP_PEER_EV_HEARTBEAT_FAILURE,
+ UP_PEER_EV_USE_COUNT_ZERO,
+ UP_PEER_EV_SESSION_TERM,
+};
+
+struct up_peer {
+ struct llist_head entry;
+
+ struct osmo_fsm_inst *fi;
+ struct up_endpoint *up_endpoint;
+
+ /* peer's remote address */
+ struct osmo_sockaddr remote_addr;
+ struct osmo_pfcp_ie_node_id remote_node_id;
+ uint32_t remote_recovery_timestamp;
+
+ struct osmo_pfcp_ie_up_function_features local_up_features;
+ struct osmo_pfcp_ie_cp_function_features peer_cp_features;
+
+ uint32_t next_seq_nr;
+
+ struct osmo_fsm_inst *heartbeat_fi;
+
+ struct osmo_use_count use_count;
+ struct osmo_use_count_entry use_count_buf[5];
+
+ DECLARE_HASHTABLE(sessions_by_up_seid, 6);
+ DECLARE_HASHTABLE(sessions_by_cp_seid, 6);
+};
+
+struct up_peer *up_peer_find_or_add(struct up_endpoint *up_ep, const struct osmo_sockaddr *remote_addr);
+struct up_peer *up_peer_find(struct up_endpoint *up_ep, const struct osmo_sockaddr *remote_addr);
+
+void up_peer_set_msg_ctx(struct up_peer *peer, struct osmo_pfcp_msg *m);
+
+char *up_peer_remote_addr_str(struct up_peer *peer);
+
+struct osmo_pfcp_msg *up_peer_init_tx(struct up_peer *peer, struct osmo_pfcp_msg *in_reply_to,
+ enum osmo_pfcp_message_type message_type);
+
+void up_peer_free(struct up_peer *peer);
diff --git a/include/osmocom/upf/up_session.h b/include/osmocom/upf/up_session.h
new file mode 100644
index 0000000..5b514a7
--- /dev/null
+++ b/include/osmocom/upf/up_session.h
@@ -0,0 +1,117 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+#include <osmocom/core/hashtable.h>
+#include <osmocom/core/use_count.h>
+
+#include <osmocom/pfcp/pfcp_msg.h>
+
+struct osmo_fsm_inst;
+struct osmo_pfcp_msg;
+struct up_peer;
+
+enum up_session_fsm_event {
+ UP_SESSION_EV_RX_SESSION_EST_REQ,
+ UP_SESSION_EV_RX_SESSION_MOD_REQ,
+ UP_SESSION_EV_RX_SESSION_DEL_REQ,
+ UP_SESSION_EV_USE_COUNT_ZERO,
+};
+
+enum up_session_kind {
+ UP_SESSION_DROP,
+ UP_SESSION_GTP_U_ENDECAPS,
+ UP_SESSION_GTP_U_FORW,
+};
+
+struct up_session {
+ struct hlist_node node_by_up_seid;
+ struct hlist_node node_by_cp_seid;
+
+ struct osmo_fsm_inst *fi;
+ struct up_peer *up_peer;
+
+ struct osmo_pfcp_ie_f_seid cp_f_seid;
+ uint64_t up_seid;
+
+ struct osmo_use_count use_count;
+ struct osmo_use_count_entry use_count_buf[8];
+
+ struct llist_head pdrs;
+ struct llist_head fars;
+ struct llist_head chosen_f_teids;
+
+ struct llist_head active_gtp_actions;
+};
+
+struct up_session *up_session_find_or_add(struct up_peer *peer, const struct osmo_pfcp_ie_f_seid *cp_f_seid,
+ const struct osmo_pfcp_ie_f_seid *up_f_seid);
+struct up_session *up_session_find_by_up_seid(struct up_peer *peer, uint64_t up_seid);
+struct up_session *up_session_find_by_cp_f_seid(struct up_peer *peer, const struct osmo_pfcp_ie_f_seid *cp_f_seid);
+struct up_session *up_session_find_by_local_teid(struct up_peer *peer, uint32_t teid);
+
+void up_session_set_msg_ctx(struct up_session *session, struct osmo_pfcp_msg *m);
+
+char *up_session_gtp_status(struct up_session *session);
+bool up_session_is_active(struct up_session *session);
+bool up_session_is_fully_active(struct up_session *session, int *active_p, int *inactive_p);
+
+int up_session_discard(struct up_session *session);
+
+int up_session_to_str_buf(char *buf, size_t buflen, struct up_session *session);
+char *up_session_to_str_c(void *ctx, struct up_session *session);
+
+struct pdr {
+ struct llist_head entry;
+ struct up_session *session;
+
+ struct osmo_pfcp_ie_create_pdr desc;
+ struct osmo_pfcp_ie_f_teid *local_f_teid;
+ struct osmo_pfcp_ie_f_teid _local_f_teid_buf;
+
+ struct far *far;
+
+ bool rx_decaps;
+ bool forw_encaps;
+ bool forw_to_core;
+ bool forw_from_core;
+
+ struct pdr *reverse_pdr;
+ bool active;
+
+ char *inactive_reason;
+};
+
+int pdr_to_str_buf(char *buf, size_t buflen, const struct pdr *pdr);
+char *pdr_to_str_c(void *ctx, const struct pdr *pdr);
+
+struct far {
+ struct llist_head entry;
+ struct up_session *session;
+
+ struct osmo_pfcp_ie_create_far desc;
+ bool active;
+};
diff --git a/include/osmocom/upf/up_session_to_gtp.c b/include/osmocom/upf/up_session_to_gtp.c
new file mode 100644
index 0000000..161ba80
--- /dev/null
+++ b/include/osmocom/upf/up_session_to_gtp.c
@@ -0,0 +1,23 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
diff --git a/include/osmocom/upf/upf.h b/include/osmocom/upf/upf.h
index c3c7849..b8065dd 100644
--- a/include/osmocom/upf/upf.h
+++ b/include/osmocom/upf/upf.h
@@ -24,12 +24,78 @@
#pragma once
+#include <osmocom/core/tdef.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/linuxlist.h>
+
+struct osmo_tdef;
struct ctrl_handle;
+struct upf_gtp_dev;
+
+#define UPF_PFCP_LISTEN_DEFAULT "0.0.0.0"
+
+extern struct osmo_tdef_group g_upf_tdef_groups[];
+
+struct pfcp_vty_cfg {
+ char *local_addr;
+ uint16_t local_port;
+};
+
+struct gtp_vty_cfg_dev {
+ struct llist_head entry;
+
+ /* If true, osmo-upf creates the GTP device on startup. If false, the GTP device was created by the user, and we
+ * just plug into it. */
+ bool create;
+
+ /* GTP device name as shown by 'ip link', e.g. 'apn0' */
+ char *dev_name;
+
+ /* Which address the GTP device should listen on.
+ * When create == true, the GTP kernel module is created to listen on this address.
+ * Also used to listen for GTP ECHO requests using libgtp. */
+ char *local_addr;
+};
+
+struct gtp_vty_cfg {
+ /* list of struct gtp_vty_cfg_dev, GTP devices as in the config file. The actual GTP devices in use are in
+ * g_upf->gtp.devs. */
+ struct llist_head devs;
+};
+
struct g_upf {
struct ctrl_handle *ctrl;
+
+ struct {
+ struct pfcp_vty_cfg vty_cfg;
+ struct up_endpoint *ep;
+ } pfcp;
+ struct {
+ /* GTP devices as in osmo-upf.cfg */
+ struct gtp_vty_cfg vty_cfg;
+
+ /* GTP devices actually in use, list of struct upf_gtp_dev. */
+ struct llist_head devs;
+
+ struct mnl_socket *nl;
+ int32_t genl_id;
+ } gtp;
};
extern struct g_upf *g_upf;
+enum upf_log_subsys {
+ DREF,
+ DPEER,
+ DSESSION,
+ DGTP,
+};
+
void g_upf_alloc(void *ctx);
+void upf_vty_init();
+int upf_pfcp_listen();
+
+int upf_gtp_devs_open();
+void upf_gtp_devs_close();
diff --git a/include/osmocom/upf/upf_gtp.h b/include/osmocom/upf/upf_gtp.h
new file mode 100644
index 0000000..a67c850
--- /dev/null
+++ b/include/osmocom/upf/upf_gtp.h
@@ -0,0 +1,85 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/logging.h>
+
+#define LOG_GTP_DEV(DEV, LEVEL, FMT, ARGS...) \
+ LOGP(DGTP, LEVEL, "%s: " FMT, upf_gtp_dev_to_str_c(OTC_SELECT, (DEV)), ##ARGS)
+
+#define PORT_GTP0_C 3386
+#define PORT_GTP0_U 3386
+
+#define PORT_GTP1_C 2123
+#define PORT_GTP1_U 2152
+
+struct upf_gtp_dev {
+ struct llist_head entry;
+
+ /* If true, osmo-upf created this GTP device on startup and will destroy it on program exit. If false, the user
+ * has created the device and osmo-upf will not destroy it. */
+ bool created;
+
+ char *name;
+ struct {
+ bool enabled;
+ struct osmo_sockaddr local_addr;
+ struct osmo_fd ofd;
+ } gtpv0;
+ struct {
+ struct osmo_sockaddr local_addr;
+ struct osmo_fd ofd;
+ } gtpv1;
+ bool sgsn_mode;
+
+ uint32_t ifidx;
+
+ struct llist_head tunnels;
+};
+
+struct upf_gtp_tun_desc {
+ uint32_t local_teid;
+ uint32_t remote_teid;
+ struct osmo_sockaddr ue_addr;
+ struct osmo_sockaddr gtp_remote_addr;
+};
+
+int upf_gtp_tun_desc_cmp(const struct upf_gtp_tun_desc *a, const struct upf_gtp_tun_desc *b);
+
+int upf_gtp_genl_open();
+void upf_gtp_genl_close();
+
+int upf_gtp_dev_open(const char *name, bool create_gtp_dev, const char *local_addr, bool listen_for_gtpv0,
+ bool sgsn_mode);
+struct upf_gtp_dev *upf_gtp_dev_find_by_name(const char *name);
+struct upf_gtp_dev *upf_gtp_dev_first();
+
+int upf_gtp_dev_tunnel_add(struct upf_gtp_dev *dev, const struct upf_gtp_tun_desc *t);
+bool upf_gtp_dev_is_tunnel_active(struct upf_gtp_dev *dev, const struct upf_gtp_tun_desc *t);
+int upf_gtp_dev_tunnel_del(struct upf_gtp_dev *dev, const struct upf_gtp_tun_desc *t);
+
+int upf_gtp_dev_to_str_buf(char *buf, size_t buflen, const struct upf_gtp_dev *dev);
+char *upf_gtp_dev_to_str_c(void *ctx, const struct upf_gtp_dev *dev);
diff --git a/include/osmocom/upf/upf_nft.h b/include/osmocom/upf/upf_nft.h
new file mode 100644
index 0000000..569eadb
--- /dev/null
+++ b/include/osmocom/upf/upf_nft.h
@@ -0,0 +1,48 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+#include <osmocom/core/socket.h>
+
+struct upf_nft_tunmap_desc {
+ struct {
+ struct osmo_sockaddr gtp_remote_addr;
+ uint32_t local_teid;
+ uint32_t remote_teid;
+ } access;
+ struct {
+ struct osmo_sockaddr gtp_remote_addr;
+ uint32_t local_teid;
+ uint32_t remote_teid;
+ } core;
+ uint32_t id;
+};
+
+int upf_nft_init();
+int upf_nft_free();
+
+int upf_nft_tunmap_create(struct upf_nft_tunmap_desc *tunmap);
+int upf_nft_tunmap_delete(struct upf_nft_tunmap_desc *tunmap);
diff --git a/src/osmo-upf/Makefile.am b/src/osmo-upf/Makefile.am
index ec04fc7..d8aa5a0 100644
--- a/src/osmo-upf/Makefile.am
+++ b/src/osmo-upf/Makefile.am
@@ -1,6 +1,7 @@
AM_CPPFLAGS = \
$(all_includes) \
-I$(top_srcdir)/include \
+ -I$(top_builddir)/include \
-I$(top_builddir) \
$(NULL)
@@ -9,10 +10,14 @@
$(LIBOSMOCORE_CFLAGS) \
$(LIBOSMOVTY_CFLAGS) \
$(LIBOSMOCTRL_CFLAGS) \
+ $(LIBOSMOGTLV_CFLAGS) \
+ $(LIBOSMOPFCP_CFLAGS) \
+ $(LIBGTPNL_CFLAGS) \
$(COVERAGE_CFLAGS) \
$(NULL)
AM_LDFLAGS = \
+ $(LIBGTPNL_LDFLAGS) \
$(COVERAGE_LDFLAGS) \
$(NULL)
@@ -22,12 +27,21 @@
osmo_upf_SOURCES = \
osmo_upf_main.c \
+ up_endpoint.c \
+ up_gtp_action.c \
+ up_peer.c \
+ up_session.c \
upf.c \
+ upf_gtp.c \
+ upf_vty.c \
$(NULL)
osmo_upf_LDADD = \
$(LIBOSMOCORE_LIBS) \
$(LIBOSMOVTY_LIBS) \
$(LIBOSMOCTRL_LIBS) \
+ $(LIBOSMOGTLV_LIBS) \
+ $(LIBOSMOPFCP_LIBS) \
+ $(LIBGTPNL_LIBS) \
$(COVERAGE_LDFLAGS) \
$(NULL)
diff --git a/src/osmo-upf/osmo_upf_main.c b/src/osmo-upf/osmo_upf_main.c
index f81eab1..302d957 100644
--- a/src/osmo-upf/osmo_upf_main.c
+++ b/src/osmo-upf/osmo_upf_main.c
@@ -34,11 +34,16 @@
#include <osmocom/vty/cpu_sched_vty.h>
#include <osmocom/vty/telnet_interface.h>
#include <osmocom/vty/ports.h>
+#include <osmocom/vty/tdef_vty.h>
#include <osmocom/ctrl/control_if.h>
#include <osmocom/ctrl/control_vty.h>
#include <osmocom/ctrl/ports.h>
+#include <osmocom/pfcp/pfcp_endpoint.h>
+
#include <osmocom/upf/upf.h>
+#include <osmocom/upf/up_endpoint.h>
+#include <osmocom/upf/upf_gtp.h>
#define _GNU_SOURCE
#include <getopt.h>
@@ -48,6 +53,7 @@
#include <signal.h>
#include <stdio.h>
+#include <string.h>
extern void *tall_vty_ctx;
@@ -207,6 +213,30 @@
};
static const struct log_info_cat upf_default_categories[] = {
+ [DREF] = {
+ .name = "DREF",
+ .description = "Reference Counting",
+ .enabled = 0, .loglevel = LOGL_NOTICE,
+ .color = OSMO_LOGCOLOR_DARKGREY,
+ },
+ [DPEER] = {
+ .name = "DPEER",
+ .description = "PFCP peer association",
+ .enabled = 0, .loglevel = LOGL_NOTICE,
+ .color = OSMO_LOGCOLOR_YELLOW,
+ },
+ [DSESSION] = {
+ .name = "DSESSION",
+ .description = "PFCP sessions",
+ .enabled = 0, .loglevel = LOGL_NOTICE,
+ .color = OSMO_LOGCOLOR_BLUE,
+ },
+ [DGTP] = {
+ .name = "DGTP",
+ .description = "GTP tunneling",
+ .enabled = 0, .loglevel = LOGL_NOTICE,
+ .color = OSMO_LOGCOLOR_PURPLE,
+ },
};
const struct log_info log_info = {
@@ -217,19 +247,27 @@
int main(int argc, char **argv)
{
int rc;
+ void *tall_infra_ctx;
/* Track the use of talloc NULL memory contexts */
talloc_enable_null_tracking();
osmo_fsm_set_dealloc_ctx(OTC_SELECT);
- tall_upf_ctx = talloc_named_const(NULL, 1, "osmo-upf");
+ tall_infra_ctx = talloc_named_const(NULL, 1, "osmo-upf");
+ tall_upf_ctx = talloc_named_const(tall_infra_ctx, 1, "osmo-upf-main");
upf_vty_app_info.tall_ctx = tall_upf_ctx;
msgb_talloc_ctx_init(tall_upf_ctx, 0);
osmo_signal_talloc_ctx_init(tall_upf_ctx);
- osmo_init_logging2(tall_upf_ctx, &log_info);
+ osmo_init_logging2(tall_infra_ctx, &log_info);
+ log_set_print_category_hex(osmo_stderr_target, 0);
+ log_set_print_category(osmo_stderr_target, 1);
+ log_set_print_level(osmo_stderr_target, 1);
+ log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME);
+ log_set_print_filename_pos(osmo_stderr_target, LOG_FILENAME_POS_LINE_END);
+ log_set_print_extended_timestamp(osmo_stderr_target, 1);
osmo_fsm_log_timeouts(true);
osmo_fsm_log_addr(true);
@@ -246,6 +284,9 @@
osmo_talloc_vty_add_cmds();
osmo_cpu_sched_vty_init(tall_upf_ctx);
+ upf_vty_init();
+ osmo_tdef_vty_groups_init(CONFIG_NODE, g_upf_tdef_groups);
+
/* Parse options */
handle_options(argc, argv);
@@ -283,6 +324,15 @@
}
}
+ if (upf_gtp_genl_open())
+ return -1;
+
+ if (upf_gtp_devs_open())
+ return -1;
+
+ if (upf_pfcp_listen())
+ return -1;
+
do {
log_reset_context();
osmo_select_main_ctx(0);
@@ -301,12 +351,20 @@
}
} while (!osmo_select_shutdown_done());
- log_fini();
+ up_endpoint_free(&g_upf->pfcp.ep);
+ upf_gtp_devs_close();
+
+ upf_gtp_genl_close();
/* Report the heap state of talloc contexts, then free, so both ASAN and Valgrind are happy... */
talloc_report_full(tall_upf_ctx, stderr);
talloc_free(tall_upf_ctx);
+ log_fini();
+
+ talloc_report_full(tall_infra_ctx, stderr);
+ talloc_free(tall_infra_ctx);
+
talloc_report_full(tall_vty_ctx, stderr);
talloc_free(tall_vty_ctx);
diff --git a/src/osmo-upf/up_endpoint.c b/src/osmo-upf/up_endpoint.c
new file mode 100644
index 0000000..f1861b8
--- /dev/null
+++ b/src/osmo-upf/up_endpoint.c
@@ -0,0 +1,323 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/pfcp/pfcp_endpoint.h>
+#include <osmocom/pfcp/pfcp_msg.h>
+
+#include <osmocom/upf/up_endpoint.h>
+#include <osmocom/upf/up_peer.h>
+#include <osmocom/upf/up_session.h>
+
+static void up_endpoint_set_msg_ctx(struct osmo_pfcp_endpoint *ep, struct osmo_pfcp_msg *m, struct osmo_pfcp_msg *req)
+{
+ struct up_endpoint *up_ep = ep->priv;
+ struct up_peer *peer;
+
+ /* If this is a response to an earlier request, just take the msg context from the request message. */
+ if (req) {
+ if (!m->ctx.peer_fi && req->ctx.peer_fi)
+ up_peer_set_msg_ctx(req->ctx.peer_fi->priv, m);
+ if (!m->ctx.session_fi && req->ctx.session_fi)
+ up_session_set_msg_ctx(req->ctx.peer_fi->priv, m);
+ }
+
+ /* From the remote address, find the matching peer instance */
+ if (!m->ctx.peer_fi) {
+ peer = up_peer_find(up_ep, &m->remote_addr);
+ if (peer) {
+ up_peer_set_msg_ctx(peer, m);
+ }
+ } else {
+ peer = m->ctx.peer_fi->priv;
+ }
+
+ /* Find a session, if the header is parsed yet and contains a SEID */
+ if (peer && !m->ctx.session_fi && m->h.seid_present) {
+ struct up_session *session;
+ session = up_session_find_by_up_seid(peer, m->h.seid);
+ if (session) {
+ up_session_set_msg_ctx(session, m);
+ }
+ }
+}
+
+static void up_ep_rx_not_impl_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m,
+ enum osmo_pfcp_message_type resp_msgt,
+ const struct osmo_pfcp_ie_node_id *node_id, enum osmo_pfcp_cause cause)
+{
+ struct osmo_pfcp_msg *tx;
+ enum osmo_pfcp_cause *tx_cause;
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "message type not implemented\n");
+ tx = osmo_pfcp_msg_alloc_tx(OTC_SELECT, NULL, &up_ep->pfcp_ep->cfg.local_node_id, m, resp_msgt);
+ tx_cause = osmo_pfcp_msg_cause(tx);
+ if (tx_cause)
+ *tx_cause = cause;
+ osmo_pfcp_endpoint_tx(up_ep->pfcp_ep, tx);
+ return;
+}
+
+static void up_ep_rx_pfd_mgmt_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ up_ep_rx_not_impl_req(up_ep, m, OSMO_PFCP_MSGT_PFD_MGMT_RESP, NULL, OSMO_PFCP_CAUSE_SERVICE_NOT_SUPPORTED);
+}
+
+static void up_ep_rx_assoc_setup_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ struct up_peer *peer = m->ctx.peer_fi ? m->ctx.peer_fi->priv : NULL;
+ if (!peer) {
+ peer = up_peer_find_or_add(up_ep, &m->remote_addr);
+ OSMO_ASSERT(peer);
+ }
+ osmo_fsm_inst_dispatch(peer->fi, UP_PEER_EV_RX_ASSOC_SETUP_REQ, (void*)m);
+}
+
+static void up_ep_rx_assoc_upd_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ if (!m->ctx.peer_fi) {
+ struct osmo_pfcp_msg *tx;
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Peer is not associated, cannot update association\n");
+ tx = osmo_pfcp_msg_alloc_tx(OTC_SELECT, NULL, &up_ep->pfcp_ep->cfg.local_node_id, m, OSMO_PFCP_MSGT_ASSOC_UPDATE_RESP);
+ /* FIXME set node_id, cause */
+ osmo_pfcp_endpoint_tx(up_ep->pfcp_ep, tx);
+ return;
+ }
+ osmo_fsm_inst_dispatch(m->ctx.peer_fi, UP_PEER_EV_RX_ASSOC_UPD_REQ, (void*)m);
+}
+
+static void up_ep_rx_assoc_rel_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ if (!m->ctx.peer_fi) {
+ struct osmo_pfcp_msg *tx;
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Peer is not associated. Sending ACK response anyway\n");
+ tx = osmo_pfcp_msg_alloc_tx(OTC_SELECT, NULL, &up_ep->pfcp_ep->cfg.local_node_id, m, OSMO_PFCP_MSGT_ASSOC_RELEASE_RESP);
+ /* FIXME set node_id, cause */
+ osmo_pfcp_endpoint_tx(up_ep->pfcp_ep, tx);
+ return;
+ }
+ osmo_fsm_inst_dispatch(m->ctx.peer_fi, UP_PEER_EV_RX_ASSOC_REL_REQ, (void*)m);
+}
+
+static void up_ep_rx_node_report_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ up_ep_rx_not_impl_req(up_ep, m, OSMO_PFCP_MSGT_NODE_REPORT_RESP, NULL /* FIXME? */,
+ OSMO_PFCP_CAUSE_SERVICE_NOT_SUPPORTED);
+}
+
+static void up_ep_rx_session_set_del_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ up_ep_rx_not_impl_req(up_ep, m, OSMO_PFCP_MSGT_SESSION_SET_DEL_RESP,
+ &up_ep->pfcp_ep->cfg.local_node_id, OSMO_PFCP_CAUSE_SERVICE_NOT_SUPPORTED);
+}
+
+static void up_ep_rx_session_est_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ if (!m->ctx.peer_fi) {
+ struct osmo_pfcp_msg *tx;
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Peer is not associated, cannot establish session\n");
+ tx = osmo_pfcp_msg_alloc_tx(OTC_SELECT, NULL, &up_ep->pfcp_ep->cfg.local_node_id, m, OSMO_PFCP_MSGT_SESSION_EST_RESP);
+ tx->ies.session_est_resp.cause = OSMO_PFCP_CAUSE_NO_ESTABLISHED_PFCP_ASSOC;
+ osmo_pfcp_endpoint_tx(up_ep->pfcp_ep, tx);
+ return;
+ }
+ osmo_fsm_inst_dispatch(m->ctx.peer_fi, UP_PEER_EV_RX_SESSION_EST_REQ, (void*)m);
+}
+
+static void up_ep_rx_session_mod_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ if (!m->ctx.session_fi) {
+ /* Session not found. */
+ struct osmo_pfcp_msg *tx;
+ tx = osmo_pfcp_msg_alloc_tx(OTC_SELECT, NULL, &up_ep->pfcp_ep->cfg.local_node_id, m,
+ OSMO_PFCP_MSGT_SESSION_MOD_RESP);
+ if (!m->ctx.peer_fi) {
+ /* Not even the peer is associated. */
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Peer is not associated, cannot modify session\n");
+ tx->ies.session_mod_resp.cause = OSMO_PFCP_CAUSE_NO_ESTABLISHED_PFCP_ASSOC;
+ } else {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR,
+ "No established session with SEID=0x%"PRIx64", cannot modify\n",
+ m->h.seid);
+ tx->ies.session_mod_resp.cause = OSMO_PFCP_CAUSE_SESSION_CTX_NOT_FOUND;
+ }
+ osmo_pfcp_endpoint_tx(up_ep->pfcp_ep, tx);
+ return;
+ }
+ osmo_fsm_inst_dispatch(m->ctx.session_fi, UP_SESSION_EV_RX_SESSION_MOD_REQ, (void*)m);
+}
+
+static void up_ep_rx_session_del_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ if (!m->ctx.session_fi) {
+ /* Session not found. */
+ struct osmo_pfcp_msg *tx;
+ tx = osmo_pfcp_msg_alloc_tx(OTC_SELECT, NULL, &up_ep->pfcp_ep->cfg.local_node_id, m, OSMO_PFCP_MSGT_SESSION_DEL_RESP);
+ if (!m->ctx.peer_fi) {
+ /* Not even the peer is associated. */
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Peer is not associated, cannot delete session\n");
+ tx->ies.session_del_resp.cause = OSMO_PFCP_CAUSE_NO_ESTABLISHED_PFCP_ASSOC;
+ } else {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR,
+ "No established session with SEID=0x%"PRIx64", cannot delete\n",
+ m->h.seid);
+ tx->ies.session_del_resp.cause = OSMO_PFCP_CAUSE_SESSION_CTX_NOT_FOUND;
+ }
+ osmo_pfcp_endpoint_tx(up_ep->pfcp_ep, tx);
+ return;
+ }
+ osmo_fsm_inst_dispatch(m->ctx.session_fi, UP_SESSION_EV_RX_SESSION_DEL_REQ, (void*)m);
+}
+
+static void up_ep_rx_session_rep_req(struct up_endpoint *up_ep, const struct osmo_pfcp_msg *m)
+{
+ up_ep_rx_not_impl_req(up_ep, m, OSMO_PFCP_MSGT_SESSION_REP_RESP, NULL, OSMO_PFCP_CAUSE_SERVICE_NOT_SUPPORTED);
+}
+
+static void up_endpoint_rx_cb(struct osmo_pfcp_endpoint *ep, struct osmo_pfcp_msg *m, struct osmo_pfcp_msg *req)
+{
+ struct up_endpoint *up_ep = ep->priv;
+
+ switch (m->h.message_type) {
+ case OSMO_PFCP_MSGT_PFD_MGMT_REQ:
+ up_ep_rx_pfd_mgmt_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_ASSOC_SETUP_REQ:
+ up_ep_rx_assoc_setup_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_ASSOC_UPDATE_REQ:
+ up_ep_rx_assoc_upd_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_ASSOC_RELEASE_REQ:
+ up_ep_rx_assoc_rel_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_NODE_REPORT_REQ:
+ up_ep_rx_node_report_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_SESSION_SET_DEL_REQ:
+ up_ep_rx_session_set_del_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_SESSION_EST_REQ:
+ up_ep_rx_session_est_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_SESSION_MOD_REQ:
+ up_ep_rx_session_mod_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_SESSION_DEL_REQ:
+ up_ep_rx_session_del_req(up_ep, m);
+ return;
+ case OSMO_PFCP_MSGT_SESSION_REP_REQ:
+ up_ep_rx_session_rep_req(up_ep, m);
+ return;
+ default:
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Unknown message type\n");
+ return;
+ }
+}
+
+struct up_endpoint *up_endpoint_init(void *ctx, const struct osmo_sockaddr *local_addr)
+{
+ int rc;
+ struct up_endpoint *up_ep;
+ up_ep = talloc_zero(ctx, struct up_endpoint);
+ INIT_LLIST_HEAD(&up_ep->peers);
+
+ up_ep->pfcp_ep = osmo_pfcp_endpoint_create(up_ep, up_ep);
+ up_ep->pfcp_ep->cfg.local_addr = *local_addr;
+
+ up_ep->pfcp_ep->set_msg_ctx = up_endpoint_set_msg_ctx;
+ up_ep->pfcp_ep->rx_msg = up_endpoint_rx_cb;
+
+ osmo_pfcp_ie_node_id_from_osmo_sockaddr(&up_ep->pfcp_ep->cfg.local_node_id, local_addr);
+
+ rc = osmo_pfcp_endpoint_bind(up_ep->pfcp_ep);
+ if (rc) {
+ talloc_free(up_ep);
+ return NULL;
+ }
+ return up_ep;
+}
+
+static struct up_session *up_endpoint_find_session(struct up_endpoint *ep, uint64_t up_seid)
+{
+ struct up_peer *peer;
+ llist_for_each_entry(peer, &ep->peers, entry) {
+ struct up_session *session = up_session_find_by_up_seid(peer, up_seid);
+ if (session)
+ return session;
+ }
+ return NULL;
+}
+
+static struct up_session *up_endpoint_find_session_by_local_teid(struct up_endpoint *ep, uint32_t teid)
+{
+ struct up_peer *peer;
+ llist_for_each_entry(peer, &ep->peers, entry) {
+ struct up_session *session = up_session_find_by_local_teid(peer, teid);
+ if (session)
+ return session;
+ }
+ return NULL;
+}
+
+uint64_t up_endpoint_next_seid(struct up_endpoint *ep)
+{
+ uint64_t sanity;
+ for (sanity = 2342; sanity; sanity--) {
+ uint64_t next_seid = osmo_pfcp_next_seid(&ep->next_seid_state);
+ if (up_endpoint_find_session(ep, next_seid))
+ continue;
+ return next_seid;
+ }
+ return 0;
+}
+
+static uint32_t up_endpoint_inc_teid(struct up_endpoint *ep)
+{
+ ep->next_teid_state++;
+ if (!ep->next_teid_state)
+ ep->next_teid_state++;
+ return ep->next_teid_state;
+}
+
+uint32_t up_endpoint_next_teid(struct up_endpoint *ep)
+{
+ uint32_t sanity;
+ for (sanity = 2342; sanity; sanity--) {
+ uint32_t next_teid = up_endpoint_inc_teid(ep);
+ if (up_endpoint_find_session_by_local_teid(ep, next_teid))
+ continue;
+ return next_teid;
+ }
+ return 0;
+}
+
+void up_endpoint_free(struct up_endpoint **_ep)
+{
+ struct up_peer *peer;
+ struct up_endpoint *ep = *_ep;
+
+ while ((peer = llist_first_entry_or_null(&ep->peers, struct up_peer, entry)))
+ up_peer_free(peer);
+
+ osmo_pfcp_endpoint_free(&ep->pfcp_ep);
+ *_ep = NULL;
+}
diff --git a/src/osmo-upf/up_gtp_action.c b/src/osmo-upf/up_gtp_action.c
new file mode 100644
index 0000000..840944e
--- /dev/null
+++ b/src/osmo-upf/up_gtp_action.c
@@ -0,0 +1,159 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <errno.h>
+#include <string.h>
+
+#include <osmocom/core/utils.h>
+
+#include <osmocom/upf/upf.h>
+#include <osmocom/upf/up_gtp_action.h>
+#include <osmocom/upf/up_peer.h>
+#include <osmocom/upf/up_session.h>
+
+int up_gtp_action_cmp(const struct up_gtp_action *a, const struct up_gtp_action *b)
+{
+ int cmp;
+ if (a == b)
+ return 0;
+ if (!a)
+ return -1;
+ if (!b)
+ return 1;
+
+#define CMP_RET(MEMB) do { \
+ int _cmp = OSMO_CMP(a->MEMB, b->MEMB); \
+ if (_cmp) \
+ return _cmp; \
+ } while (0)
+
+ CMP_RET(kind);
+
+ switch (a->kind) {
+ case UP_GTP_U_ENDECAPS:
+ CMP_RET(endecaps.local_teid);
+ CMP_RET(endecaps.remote_teid);
+ cmp = osmo_sockaddr_cmp(&a->endecaps.gtp_remote_addr, &b->endecaps.gtp_remote_addr);
+ if (cmp)
+ return cmp;
+ cmp = osmo_sockaddr_cmp(&a->endecaps.ue_addr, &b->endecaps.ue_addr);
+ if (cmp)
+ return cmp;
+ break;
+
+ case UP_GTP_U_TUNMAP:
+ CMP_RET(tunmap.access.local_teid);
+ CMP_RET(tunmap.access.remote_teid);
+ CMP_RET(tunmap.core.local_teid);
+ CMP_RET(tunmap.core.remote_teid);
+ break;
+ default:
+ break;
+ }
+ return 0;
+}
+
+static int up_gtp_action_enable_disable(struct up_gtp_action *a, bool enable)
+{
+ struct upf_gtp_dev *gtp_dev;
+ int rc;
+
+ switch (a->kind) {
+ case UP_GTP_U_ENDECAPS:
+ /* use the first available GTP device.
+ * TODO: select by interface name?
+ */
+ gtp_dev = upf_gtp_dev_first();
+ if (!gtp_dev) {
+ LOG_UP_GTP_ACTION(a, LOGL_ERROR, "No GTP device open, cannot %s\n", enable ? "enable" : "disable");
+ return -EIO;
+ }
+
+ if (enable)
+ rc = upf_gtp_dev_tunnel_add(gtp_dev, &a->endecaps);
+ else
+ rc = upf_gtp_dev_tunnel_del(gtp_dev, &a->endecaps);
+ if (rc) {
+ LOG_UP_GTP_ACTION(a, LOGL_ERROR, "Failed to %s GTP tunnel: %d %s\n",
+ enable ? "enable" : "disable", rc, strerror(-rc));
+ return rc;
+ }
+ LOG_UP_GTP_ACTION(a, LOGL_NOTICE, "%s GTP tunnel\n", enable ? "Enabled" : "Disabled");
+ return 0;
+ case UP_GTP_U_TUNMAP:
+ LOG_UP_GTP_ACTION(a, LOGL_ERROR, "TEID translation not yet implemented\n");
+ return -ENOTSUP;
+ default:
+ LOG_UP_GTP_ACTION(a, LOGL_ERROR, "Invalid action\n");
+ return -ENOTSUP;
+ }
+}
+
+int up_gtp_action_enable(struct up_gtp_action *a)
+{
+ return up_gtp_action_enable_disable(a, true);
+}
+
+int up_gtp_action_disable(struct up_gtp_action *a)
+{
+ return up_gtp_action_enable_disable(a, false);
+}
+
+int up_gtp_action_to_str_buf(char *buf, size_t buflen, const struct up_gtp_action *a)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ switch (a->kind) {
+ case UP_GTP_U_ENDECAPS:
+ OSMO_STRBUF_PRINTF(sb, "GTP:endecaps GTP-access:");
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &a->endecaps.gtp_remote_addr);
+ OSMO_STRBUF_PRINTF(sb, " TEID-r:0x%"PRIx32" TEID-l:0x%"PRIx32" IP-core:",
+ a->endecaps.remote_teid, a->endecaps.local_teid);
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &a->endecaps.ue_addr);
+ break;
+ case UP_GTP_U_TUNMAP:
+ OSMO_STRBUF_PRINTF(sb, "GTP:tunmap GTP-access:");
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &a->tunmap.access.gtp_remote_addr);
+ OSMO_STRBUF_PRINTF(sb, " TEID-access-r:0x%"PRIx32" TEID-access-l:0x%"PRIx32" GTP-core:",
+ a->tunmap.access.remote_teid, a->tunmap.access.local_teid);
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &a->tunmap.core.gtp_remote_addr);
+ OSMO_STRBUF_PRINTF(sb, " TEID-core-r:0x%"PRIx32" TEID-core-l:0x%"PRIx32,
+ a->tunmap.core.remote_teid, a->tunmap.core.local_teid);
+ break;
+ case UP_GTP_DROP:
+ OSMO_STRBUF_PRINTF(sb, "GTP:drop");
+ break;
+ default:
+ OSMO_STRBUF_PRINTF(sb, "GTP:?");
+ break;
+ }
+ if (a->session)
+ OSMO_STRBUF_PRINTF(sb, " PFCP-peer:%s SEID-l:0x%"PRIx64" PDR:%d,%d",
+ up_peer_remote_addr_str(a->session->up_peer),
+ a->session->up_seid, a->pdr_core, a->pdr_access);
+ return sb.chars_needed;
+}
+
+char *up_gtp_action_to_str_c(void *ctx, const struct up_gtp_action *a)
+{
+ OSMO_NAME_C_IMPL(ctx, 128, "ERROR", up_gtp_action_to_str_buf, a)
+}
diff --git a/src/osmo-upf/up_peer.c b/src/osmo-upf/up_peer.c
new file mode 100644
index 0000000..0c0ac77
--- /dev/null
+++ b/src/osmo-upf/up_peer.c
@@ -0,0 +1,545 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <errno.h>
+
+#include <osmocom/core/fsm.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/tdef.h>
+#include <osmocom/core/utils.h>
+
+#include <osmocom/pfcp/pfcp_msg.h>
+#include <osmocom/pfcp/pfcp_endpoint.h>
+
+#include <osmocom/upf/upf.h>
+#include <osmocom/upf/up_peer.h>
+#include <osmocom/upf/up_endpoint.h>
+#include <osmocom/upf/up_session.h>
+
+enum up_peer_fsm_state {
+ UP_PEER_ST_NOT_ASSOCIATED,
+ UP_PEER_ST_ASSOCIATED,
+ UP_PEER_ST_GRACEFUL_RELEASE,
+ UP_PEER_ST_WAIT_USE_COUNT,
+};
+
+static const struct value_string up_peer_fsm_event_names[] = {
+ OSMO_VALUE_STRING(UP_PEER_EV_RX_ASSOC_SETUP_REQ),
+ OSMO_VALUE_STRING(UP_PEER_EV_RX_ASSOC_UPD_REQ),
+ OSMO_VALUE_STRING(UP_PEER_EV_RX_ASSOC_REL_REQ),
+ OSMO_VALUE_STRING(UP_PEER_EV_RX_SESSION_EST_REQ),
+ OSMO_VALUE_STRING(UP_PEER_EV_HEARTBEAT_FAILURE),
+ OSMO_VALUE_STRING(UP_PEER_EV_USE_COUNT_ZERO),
+ OSMO_VALUE_STRING(UP_PEER_EV_SESSION_TERM),
+ {}
+};
+
+static struct osmo_fsm up_peer_fsm;
+
+static const struct osmo_tdef_state_timeout up_peer_fsm_timeouts[32] = {
+ [UP_PEER_ST_GRACEFUL_RELEASE] = { .T = -21 },
+};
+
+/* Transition to a state, using the T timer defined in up_peer_fsm_timeouts.
+ * Assumes local variable fi exists. */
+#define up_peer_fsm_state_chg(state) \
+ osmo_tdef_fsm_inst_state_chg(fi, state, \
+ up_peer_fsm_timeouts, \
+ osmo_pfcp_tdefs, \
+ 5)
+
+static int up_peer_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
+{
+ struct up_peer *peer = e->use_count->talloc_object;
+ int32_t total;
+ int level;
+
+ if (!e->use)
+ return -EINVAL;
+
+ total = osmo_use_count_total(&peer->use_count);
+
+ if (total == 0
+ || (total == 1 && old_use_count == 0 && e->count == 1))
+ level = LOGL_INFO;
+ else
+ level = LOGL_DEBUG;
+
+ LOGPFSMSLSRC(peer->fi, DREF, level, file, line,
+ "%s %s: now used by %s\n",
+ (e->count - old_use_count) > 0? "+" : "-", e->use,
+ osmo_use_count_to_str_c(OTC_SELECT, &peer->use_count));
+
+ if (e->count < 0)
+ return -ERANGE;
+
+ if (total == 0)
+ osmo_fsm_inst_dispatch(peer->fi, UP_PEER_EV_USE_COUNT_ZERO, NULL);
+ return 0;
+}
+
+char *up_peer_remote_addr_str(struct up_peer *peer)
+{
+ struct osmo_sockaddr remote_addr = peer->remote_addr;
+#if 1
+ /* Zero the port, it is not interesting information. The port for PFCP is defined fixed, and there is no use
+ * printing it in the logs */
+ osmo_sockaddr_set_port(&remote_addr.u.sa, 0);
+#endif
+ return osmo_sockaddr_to_str_c(OTC_SELECT, &remote_addr);
+}
+
+static void up_peer_update_id(struct up_peer *peer)
+{
+ osmo_fsm_inst_update_id_f_sanitize(peer->fi, '-', "%s", up_peer_remote_addr_str(peer));
+ LOGPFSML(peer->fi, LOGL_DEBUG, "Updated id\n");
+}
+
+static struct up_peer *up_peer_add(struct up_endpoint *up_endpoint, const struct osmo_sockaddr *remote_addr)
+{
+ struct up_peer *peer;
+
+ struct osmo_fsm_inst *fi = osmo_fsm_inst_alloc(&up_peer_fsm, up_endpoint, NULL, LOGL_DEBUG, NULL);
+ OSMO_ASSERT(fi);
+
+ peer = talloc(fi, struct up_peer);
+ OSMO_ASSERT(peer);
+ fi->priv = peer;
+
+ *peer = (struct up_peer) {
+ .fi = fi,
+ .up_endpoint = up_endpoint,
+ .remote_addr = *remote_addr,
+ .heartbeat_fi = NULL /* FIXME */,
+ .use_count = {
+ .talloc_object = peer,
+ .use_cb = up_peer_use_cb,
+ },
+ };
+ osmo_use_count_make_static_entries(&peer->use_count, peer->use_count_buf, ARRAY_SIZE(peer->use_count_buf));
+ hash_init(peer->sessions_by_up_seid);
+ hash_init(peer->sessions_by_cp_seid);
+
+ osmo_pfcp_bits_set(peer->local_up_features.bits, OSMO_PFCP_UP_FEAT_BUNDL, true);
+ osmo_pfcp_bits_set(peer->local_up_features.bits, OSMO_PFCP_UP_FEAT_RTTL, true);
+ osmo_pfcp_bits_set(peer->local_up_features.bits, OSMO_PFCP_UP_FEAT_FTUP, true);
+
+ up_peer_update_id(peer);
+
+ llist_add(&peer->entry, &up_endpoint->peers);
+ return peer;
+}
+
+struct up_peer *up_peer_find(struct up_endpoint *up_endpoint, const struct osmo_sockaddr *remote_addr)
+{
+ struct up_peer *peer;
+ llist_for_each_entry(peer, &up_endpoint->peers, entry) {
+ if (osmo_sockaddr_cmp(&peer->remote_addr, remote_addr))
+ continue;
+ return peer;
+ }
+ return NULL;
+}
+
+struct up_peer *up_peer_find_or_add(struct up_endpoint *up_endpoint, const struct osmo_sockaddr *remote_addr)
+{
+ struct up_peer *peer = up_peer_find(up_endpoint, remote_addr);
+ if (peer)
+ return peer;
+ return up_peer_add(up_endpoint, remote_addr);
+}
+
+int up_peer_tx(struct up_peer *peer, struct osmo_pfcp_msg *m)
+{
+ return osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, m);
+}
+
+static int up_peer_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+ //struct up_peer *peer = fi->priv;
+ /* Return 1 to terminate FSM instance, 0 to keep running */
+ return 1;
+}
+
+void up_peer_set_msg_ctx(struct up_peer *peer, struct osmo_pfcp_msg *m)
+{
+ OSMO_ASSERT(!m->ctx.peer_fi);
+
+ m->ctx.peer_fi = peer->fi;
+ m->ctx.peer_use_count = &peer->use_count;
+ m->ctx.peer_use_token = (m->rx ? UP_USE_MSG_RX : UP_USE_MSG_TX);
+ osmo_use_count_get_put(m->ctx.peer_use_count, m->ctx.peer_use_token, 1);
+}
+
+struct osmo_pfcp_msg *up_peer_init_tx(struct up_peer *peer, struct osmo_pfcp_msg *in_reply_to,
+ enum osmo_pfcp_message_type message_type)
+{
+ struct osmo_pfcp_msg *tx = osmo_pfcp_msg_alloc_tx(OTC_SELECT, &peer->remote_addr,
+ &peer->up_endpoint->pfcp_ep->cfg.local_node_id,
+ in_reply_to, message_type);
+ up_peer_set_msg_ctx(peer, tx);
+ return tx;
+}
+
+static int up_peer_tx_assoc_setup_resp(struct up_peer *peer, struct osmo_pfcp_msg *m, enum osmo_pfcp_cause cause)
+{
+ struct osmo_pfcp_msg *resp;
+
+ resp = up_peer_init_tx(peer, m, OSMO_PFCP_MSGT_ASSOC_SETUP_RESP);
+
+ resp->ies.assoc_setup_resp = (struct osmo_pfcp_msg_assoc_setup_resp) {
+ .cause = cause,
+ .recovery_time_stamp = g_upf->pfcp.ep->pfcp_ep->recovery_time_stamp,
+ .up_function_features_present = true,
+ .up_function_features = peer->local_up_features,
+ };
+ resp->ies.assoc_setup_resp.recovery_time_stamp = g_upf->pfcp.ep->pfcp_ep->recovery_time_stamp;
+
+ if (osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, resp)) {
+ OSMO_LOG_PFCP_MSG(resp, LOGL_ERROR, "Error sending response, cannot associate with peer\n");
+ return -EIO;
+ }
+ return 0;
+}
+
+static int up_peer_tx_assoc_rel_resp(struct up_peer *peer, struct osmo_pfcp_msg *m, enum osmo_pfcp_cause cause)
+{
+ struct osmo_pfcp_msg *resp;
+
+ resp = up_peer_init_tx(peer, m, OSMO_PFCP_MSGT_ASSOC_RELEASE_RESP);
+
+ resp->ies.assoc_release_resp = (struct osmo_pfcp_msg_assoc_release_resp) {
+ .cause = cause,
+ };
+
+ if (osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, resp)) {
+ OSMO_LOG_PFCP_MSG(resp, LOGL_ERROR, "Error sending response\n");
+ return -EIO;
+ }
+ return 0;
+}
+
+static void up_peer_clear_sessions(struct up_peer *peer)
+{
+ struct up_session *session;
+ int bkt;
+ struct hlist_node *tmp;
+ int count = 0;
+ hash_for_each_safe(peer->sessions_by_up_seid, bkt, tmp, session, node_by_up_seid) {
+ count += up_session_discard(session);
+ }
+ if (count)
+ LOGPFSML(peer->fi, LOGL_NOTICE, "terminated %d sessions\n", count);
+}
+
+static void up_peer_rx_assoc_setup_req(struct up_peer *peer, struct osmo_pfcp_msg *m)
+{
+ struct osmo_fsm_inst *fi = peer->fi;
+ enum osmo_pfcp_cause cause = OSMO_PFCP_CAUSE_REQUEST_ACCEPTED;
+
+ if (fi->state == UP_PEER_ST_ASSOCIATED) {
+ /* Retransmissions of the ACK response happen in pfcp_endpoint.c. So if we get this, it is a genuine
+ * duplicate association setup request. We could reject it. But why. Just "replace" with the new
+ * association. Continue. */
+ /* If the peer has restarted, it has forgotten about all sessions. */
+ if (peer->remote_recovery_timestamp != m->ies.assoc_setup_req.recovery_time_stamp) {
+ LOGPFSML(fi, LOGL_NOTICE, "another Association Setup Request, with different Recovery Timestamp."
+ " Clearing sessions, sending ACK.\n");
+ up_peer_clear_sessions(peer);
+ } else {
+ LOGPFSML(fi, LOGL_NOTICE, "another Association Setup Request, with same Recovery Timestamp."
+ " Keeping sessions, sending ACK.\n");
+ }
+ } else if (up_peer_fsm_state_chg(UP_PEER_ST_ASSOCIATED)) {
+ /* Not allowed to transition to ST_ASSOCIATED */
+ cause = OSMO_PFCP_CAUSE_REQUEST_REJECTED;
+ } else {
+ /* Successfully transitioned to ST_ASSOCIATED */
+ peer->remote_recovery_timestamp = m->ies.assoc_setup_req.recovery_time_stamp;
+ peer->remote_node_id = m->ies.assoc_setup_req.node_id;
+ if (m->ies.assoc_setup_req.cp_function_features_present)
+ peer->peer_cp_features = m->ies.assoc_setup_req.cp_function_features;
+ }
+
+ if (up_peer_tx_assoc_setup_resp(peer, m, cause)
+ || cause != OSMO_PFCP_CAUSE_REQUEST_ACCEPTED)
+ up_peer_fsm_state_chg(UP_PEER_ST_WAIT_USE_COUNT);
+
+ LOGPFSML(fi, LOGL_NOTICE, "Peer associated, Node-Id=%s. Local UP features: [%s]; Peer CP features: [%s]\n",
+ osmo_pfcp_ie_node_id_to_str_c(OTC_SELECT, &peer->remote_node_id),
+ osmo_pfcp_bits_to_str_c(OTC_SELECT, peer->local_up_features.bits, osmo_pfcp_up_feature_strs),
+ osmo_pfcp_bits_to_str_c(OTC_SELECT, peer->peer_cp_features.bits, osmo_pfcp_cp_feature_strs));
+}
+
+static void up_peer_rx_assoc_rel_req(struct up_peer *peer, struct osmo_pfcp_msg *m)
+{
+ struct osmo_fsm_inst *fi = peer->fi;
+ up_peer_tx_assoc_rel_resp(peer, m, OSMO_PFCP_CAUSE_REQUEST_ACCEPTED);
+ up_peer_fsm_state_chg(UP_PEER_ST_WAIT_USE_COUNT);
+}
+
+static void up_peer_rx_session_est_req(struct up_peer *peer, struct osmo_pfcp_msg *m)
+{
+ enum osmo_pfcp_cause cause = OSMO_PFCP_CAUSE_REQUEST_ACCEPTED;
+ struct osmo_pfcp_msg *resp;
+ struct up_session *session = up_session_find_or_add(peer, &m->ies.session_est_req.cp_f_seid, NULL);
+
+ if (!session) {
+ cause = OSMO_PFCP_CAUSE_NO_RESOURCES_AVAILABLE;
+ goto nack_response;
+ }
+
+ up_session_set_msg_ctx(session, m);
+
+ if (osmo_fsm_inst_dispatch(session->fi, UP_SESSION_EV_RX_SESSION_EST_REQ, m)) {
+ cause = OSMO_PFCP_CAUSE_REQUEST_REJECTED;
+ goto nack_response;
+ }
+ return;
+
+nack_response:
+ resp = up_peer_init_tx(peer, m, OSMO_PFCP_MSGT_SESSION_EST_RESP);
+ resp->h.seid = m->ies.session_est_req.cp_f_seid.seid;
+ resp->h.seid_present = true;
+ resp->ies.session_est_resp = (struct osmo_pfcp_msg_session_est_resp){
+ .cause = cause,
+ };
+ osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, resp);
+}
+
+static void up_peer_not_associated_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ struct up_peer *peer = fi->priv;
+
+ switch (event) {
+
+ case UP_PEER_EV_RX_ASSOC_SETUP_REQ:
+ up_peer_rx_assoc_setup_req(peer, data);
+ break;
+
+ case UP_PEER_EV_USE_COUNT_ZERO:
+ /* Not associated and no pending messages. discard peer. */
+ up_peer_fsm_state_chg(UP_PEER_ST_WAIT_USE_COUNT);
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void up_peer_associated_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ struct up_peer *peer = fi->priv;
+
+ switch (event) {
+
+ case UP_PEER_EV_RX_ASSOC_SETUP_REQ:
+ up_peer_rx_assoc_setup_req(peer, data);
+ break;
+
+ case UP_PEER_EV_RX_ASSOC_UPD_REQ:
+ // FIXME
+ break;
+
+ case UP_PEER_EV_RX_SESSION_EST_REQ:
+ up_peer_rx_session_est_req(peer, data);
+ break;
+
+ case UP_PEER_EV_HEARTBEAT_FAILURE:
+ // FIXME
+ break;
+
+ case UP_PEER_EV_USE_COUNT_ZERO:
+ /* Stay associated. */
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void up_peer_associated_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
+{
+ struct up_peer *peer = fi->priv;
+ if (next_state != UP_PEER_ST_ASSOCIATED)
+ LOGPFSML(fi, LOGL_NOTICE, "Peer %s released\n",
+ osmo_pfcp_ie_node_id_to_str_c(OTC_SELECT, &peer->remote_node_id));
+}
+
+static void up_peer_graceful_release_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ //struct up_peer *peer = fi->priv;
+ // FIXME
+}
+
+static void up_peer_graceful_release_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ struct up_peer *peer = fi->priv;
+
+ switch (event) {
+
+ case UP_PEER_EV_HEARTBEAT_FAILURE:
+ up_peer_fsm_state_chg(UP_PEER_ST_WAIT_USE_COUNT);
+ break;
+
+ case UP_PEER_EV_USE_COUNT_ZERO:
+ /* When there are still sessions, stay around. */
+ if (!hash_empty(peer->sessions_by_up_seid))
+ return;
+ up_peer_fsm_state_chg(UP_PEER_ST_WAIT_USE_COUNT);
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void up_peer_wait_use_count_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ struct up_peer *peer = fi->priv;
+ up_peer_clear_sessions(peer);
+ if (!osmo_use_count_total(&peer->use_count))
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
+}
+
+static void up_peer_wait_use_count_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ struct up_peer *peer = fi->priv;
+ switch (event) {
+
+ case UP_PEER_EV_USE_COUNT_ZERO:
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
+ return;
+
+ case UP_PEER_EV_RX_ASSOC_SETUP_REQ:
+ up_peer_rx_assoc_setup_req(peer, data);
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state up_peer_fsm_states[] = {
+ [UP_PEER_ST_NOT_ASSOCIATED] = {
+ .name = "NOT_ASSOCIATED",
+ .in_event_mask = 0
+ | S(UP_PEER_EV_RX_ASSOC_SETUP_REQ)
+ | S(UP_PEER_EV_USE_COUNT_ZERO)
+ ,
+ .out_state_mask = 0
+ | S(UP_PEER_ST_ASSOCIATED)
+ | S(UP_PEER_ST_WAIT_USE_COUNT)
+ ,
+ .action = up_peer_not_associated_action,
+ },
+ [UP_PEER_ST_ASSOCIATED] = {
+ .name = "ASSOCIATED",
+ .in_event_mask = 0
+ | S(UP_PEER_EV_RX_ASSOC_SETUP_REQ)
+ | S(UP_PEER_EV_RX_ASSOC_UPD_REQ)
+ | S(UP_PEER_EV_RX_SESSION_EST_REQ)
+ | S(UP_PEER_EV_HEARTBEAT_FAILURE)
+ | S(UP_PEER_EV_USE_COUNT_ZERO)
+ ,
+ .out_state_mask = 0
+ | S(UP_PEER_ST_ASSOCIATED)
+ | S(UP_PEER_ST_GRACEFUL_RELEASE)
+ | S(UP_PEER_ST_WAIT_USE_COUNT)
+ ,
+ .action = up_peer_associated_action,
+ .onleave = up_peer_associated_onleave,
+ },
+ [UP_PEER_ST_GRACEFUL_RELEASE] = {
+ .name = "GRACEFUL_RELEASE",
+ .in_event_mask = 0
+ | S(UP_PEER_EV_HEARTBEAT_FAILURE)
+ | S(UP_PEER_EV_USE_COUNT_ZERO)
+ ,
+ .out_state_mask = 0
+ | S(UP_PEER_ST_WAIT_USE_COUNT)
+ ,
+ .onenter = up_peer_graceful_release_onenter,
+ .action = up_peer_graceful_release_action,
+ },
+ [UP_PEER_ST_WAIT_USE_COUNT] = {
+ .name = "WAIT_USE_COUNT",
+ .in_event_mask = 0
+ | S(UP_PEER_EV_USE_COUNT_ZERO)
+ | S(UP_PEER_EV_RX_ASSOC_SETUP_REQ)
+ ,
+ .out_state_mask = 0
+ | S(UP_PEER_ST_ASSOCIATED)
+ ,
+ .onenter = up_peer_wait_use_count_onenter,
+ .action = up_peer_wait_use_count_action,
+ },
+};
+
+void up_peer_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
+{
+ struct up_peer *peer = fi->priv;
+ LOGPFSML(fi, LOGL_NOTICE, "Peer removed\n");
+ llist_del(&peer->entry);
+}
+
+static void up_peer_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+ case UP_PEER_EV_SESSION_TERM:
+ /* ignore */
+ return;
+ case UP_PEER_EV_RX_ASSOC_REL_REQ:
+ up_peer_rx_assoc_rel_req(fi->priv, data);
+ return;
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static struct osmo_fsm up_peer_fsm = {
+ .name = "up_peer",
+ .log_subsys = DPEER,
+ .states = up_peer_fsm_states,
+ .num_states = ARRAY_SIZE(up_peer_fsm_states),
+ .event_names = up_peer_fsm_event_names,
+ .timer_cb = up_peer_fsm_timer_cb,
+ .cleanup = up_peer_fsm_cleanup,
+ .allstate_event_mask = 0
+ | S(UP_PEER_EV_RX_ASSOC_REL_REQ)
+ | S(UP_PEER_EV_SESSION_TERM)
+ ,
+ .allstate_action = up_peer_allstate_action,
+};
+
+static __attribute__((constructor)) void up_peer_fsm_register(void)
+{
+ OSMO_ASSERT(osmo_fsm_register(&up_peer_fsm) == 0);
+}
+
+void up_peer_free(struct up_peer *peer)
+{
+ osmo_fsm_inst_term(peer->fi, OSMO_FSM_TERM_REGULAR, NULL);
+}
diff --git a/src/osmo-upf/up_peer_fsm.c b/src/osmo-upf/up_peer_fsm.c
new file mode 100644
index 0000000..ec37c0b
--- /dev/null
+++ b/src/osmo-upf/up_peer_fsm.c
@@ -0,0 +1,196 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/fsm.h>
+
+#include <osmocom/upf/up_peer.h>
+
+enum up_peer_fsm_state {
+ UP_PEER_ST_NOT_ASSOCIATED,
+ UP_PEER_ST_ASSOCIATED,
+ UP_PEER_ST_GRACEFUL_RELEASE,
+};
+
+static const struct value_string up_peer_fsm_event_names[] = {
+ OSMO_VALUE_STRING(UP_PEER_EV_RX_ASSOC_SETUP_REQ),
+ OSMO_VALUE_STRING(UP_PEER_EV_RX_ASSOC_UPD_REQ),
+ OSMO_VALUE_STRING(UP_PEER_EV_RX_SESSION_EST_REQ),
+ OSMO_VALUE_STRING(UP_PEER_EV_HEARTBEAT_FAILURE),
+ {}
+};
+
+static struct osmo_fsm up_peer_fsm;
+
+static const struct osmo_tdef_state_timeout up_peer_fsm_timeouts[32] = {
+ [UP_PEER_ST_GRACEFUL_RELEASE] = { .T = -21 },
+};
+
+/* Transition to a state, using the T timer defined in up_peer_fsm_timeouts.
+ * Assumes local variable fi exists. */
+#define up_peer_fsm_state_chg(state) \
+ osmo_tdef_fsm_inst_state_chg(fi, state, \
+ up_peer_fsm_timeouts, \
+ g_upf_tdefs, \
+ 5)
+
+struct up_peer *up_peer_alloc(struct osmo_fsm_inst *parent_fi, uint32_t parent_event_term)
+{
+ struct up_peer *up_peer;
+
+ struct osmo_fsm_inst *fi = osmo_fsm_inst_alloc_child(&up_peer_fsm, parent_fi, parent_event_term);
+ OSMO_ASSERT(fi);
+
+ up_peer = talloc(fi, struct up_peer);
+ OSMO_ASSERT(up_peer);
+ fi->priv = up_peer;
+ *up_peer = (struct up_peer){
+ .fi = fi,
+ };
+
+ return up_peer;
+}
+
+static int up_peer_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+ //struct up_peer *up_peer = fi->priv;
+ /* Return 1 to terminate FSM instance, 0 to keep running */
+ return 1;
+}
+
+static void up_peer_not_associated_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ //struct up_peer *up_peer = fi->priv;
+
+ switch (event) {
+
+ case UP_PEER_EV_RX_ASSOC_SETUP_REQ:
+ // FIXME
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void up_peer_associated_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ //struct up_peer *up_peer = fi->priv;
+ // FIXME
+}
+
+static void up_peer_associated_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ //struct up_peer *up_peer = fi->priv;
+
+ switch (event) {
+
+ case UP_PEER_EV_RX_ASSOC_UPD_REQ:
+ // FIXME
+ break;
+
+ case UP_PEER_EV_RX_SESSION_EST_REQ:
+ // FIXME
+ break;
+
+ case UP_PEER_EV_HEARTBEAT_FAILURE:
+ // FIXME
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void up_peer_graceful_release_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ //struct up_peer *up_peer = fi->priv;
+ // FIXME
+}
+
+static void up_peer_graceful_release_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ //struct up_peer *up_peer = fi->priv;
+
+ switch (event) {
+
+ case UP_PEER_EV_HEARTBEAT_FAILURE:
+ // FIXME
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state up_peer_fsm_states[] = {
+ [UP_PEER_ST_NOT_ASSOCIATED] = {
+ .name = "not_associated",
+ .in_event_mask = 0
+ | S(UP_PEER_EV_RX_ASSOC_SETUP_REQ)
+ ,
+ .out_state_mask = 0
+ | S(UP_PEER_ST_ASSOCIATED)
+ ,
+ .action = up_peer_not_associated_action,
+ },
+ [UP_PEER_ST_ASSOCIATED] = {
+ .name = "associated",
+ .in_event_mask = 0
+ | S(UP_PEER_EV_RX_ASSOC_UPD_REQ)
+ | S(UP_PEER_EV_RX_SESSION_EST_REQ)
+ | S(UP_PEER_EV_HEARTBEAT_FAILURE)
+ ,
+ .out_state_mask = 0
+ | S(UP_PEER_ST_GRACEFUL_RELEASE)
+ ,
+ .onenter = up_peer_associated_onenter,
+ .action = up_peer_associated_action,
+ },
+ [UP_PEER_ST_GRACEFUL_RELEASE] = {
+ .name = "graceful_release",
+ .in_event_mask = 0
+ | S(UP_PEER_EV_HEARTBEAT_FAILURE)
+ ,
+ .out_state_mask = 0
+ ,
+ .onenter = up_peer_graceful_release_onenter,
+ .action = up_peer_graceful_release_action,
+ },
+};
+
+static struct osmo_fsm up_peer_fsm = {
+ .name = "up_peer",
+ .states = up_peer_fsm_states,
+ .num_states = ARRAY_SIZE(up_peer_fsm_states),
+ .log_subsys = DSESSION,
+ .event_names = up_peer_fsm_event_names,
+ .timer_cb = up_peer_fsm_timer_cb,
+};
+
+static __attribute__((constructor)) void up_peer_fsm_register(void)
+{
+ OSMO_ASSERT(osmo_fsm_register(&up_peer_fsm) == 0);
+}
diff --git a/src/osmo-upf/up_session.c b/src/osmo-upf/up_session.c
new file mode 100644
index 0000000..d1479b1
--- /dev/null
+++ b/src/osmo-upf/up_session.c
@@ -0,0 +1,1358 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <errno.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/fsm.h>
+
+#include <osmocom/pfcp/pfcp_msg.h>
+#include <osmocom/pfcp/pfcp_endpoint.h>
+
+#include <osmocom/upf/upf.h>
+#include <osmocom/upf/up_endpoint.h>
+#include <osmocom/upf/up_peer.h>
+#include <osmocom/upf/up_session.h>
+#include <osmocom/upf/up_gtp_action.h>
+
+static enum osmo_pfcp_cause up_session_setup_gtp(struct up_session *session);
+
+void up_session_set_msg_ctx(struct up_session *session, struct osmo_pfcp_msg *m)
+{
+ if (!m->ctx.peer_fi)
+ up_peer_set_msg_ctx(session->up_peer, m);
+
+ OSMO_ASSERT(!m->ctx.session_fi);
+
+ m->ctx.session_fi = session->fi;
+ m->ctx.session_use_count = &session->use_count;
+ m->ctx.session_use_token = (m->rx ? UP_USE_MSG_RX : UP_USE_MSG_TX);
+ osmo_use_count_get_put(m->ctx.session_use_count, m->ctx.session_use_token, 1);
+}
+
+enum up_session_fsm_state {
+ UP_SESSION_ST_INIT,
+ UP_SESSION_ST_ESTABLISHED,
+ UP_SESSION_ST_WAIT_USE_COUNT,
+};
+
+static const struct value_string up_session_fsm_event_names[] = {
+ OSMO_VALUE_STRING(UP_SESSION_EV_RX_SESSION_EST_REQ),
+ OSMO_VALUE_STRING(UP_SESSION_EV_RX_SESSION_MOD_REQ),
+ OSMO_VALUE_STRING(UP_SESSION_EV_RX_SESSION_DEL_REQ),
+ OSMO_VALUE_STRING(UP_SESSION_EV_USE_COUNT_ZERO),
+ {}
+};
+
+static struct osmo_fsm up_session_fsm;
+
+static const struct osmo_tdef_state_timeout up_session_fsm_timeouts[32] = {
+ [UP_SESSION_ST_INIT] = { .T = 0 },
+ [UP_SESSION_ST_ESTABLISHED] = { .T = 0 },
+ [UP_SESSION_ST_WAIT_USE_COUNT] = { .T = 0 },
+};
+
+/* Transition to a state, using the T timer defined in up_session_fsm_timeouts.
+ * Assumes local variable fi exists. */
+#define up_session_fsm_state_chg(STATE) do { \
+ if (fi->state != STATE) \
+ osmo_tdef_fsm_inst_state_chg(fi, STATE, \
+ up_session_fsm_timeouts, \
+ osmo_pfcp_tdefs, \
+ 5); \
+ } while (0)
+
+static int up_session_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+ /* Return 1 to terminate FSM instance, 0 to keep running */
+ return 1;
+}
+
+struct osmo_pfcp_msg *up_session_init_tx(struct up_session *session, struct osmo_pfcp_msg *in_reply_to,
+ enum osmo_pfcp_message_type message_type)
+{
+ struct osmo_pfcp_msg *tx = up_peer_init_tx(session->up_peer, in_reply_to, message_type);
+ tx->h.seid = session->cp_f_seid.seid;
+ tx->h.seid_present = true;
+ up_session_set_msg_ctx(session, tx);
+ return tx;
+}
+
+struct chosen_f_teid {
+ struct llist_head entry;
+ uint8_t choose_id;
+ struct osmo_pfcp_ie_f_teid f_teid;
+};
+
+struct chosen_f_teid *chosen_f_teid_find(struct llist_head *list, uint8_t choose_id)
+{
+ struct chosen_f_teid *chosen;
+ llist_for_each_entry(chosen, list, entry) {
+ if (chosen->choose_id == choose_id)
+ return chosen;
+ }
+ return NULL;
+}
+
+/* Choose an F-TEID (when the peer has sent CHOOSE = 1).
+ * If the peer also sent a CHOOSE_ID, then remember this F-TEID choice under the given ID, and re-use that choice when
+ * the same ID re-appears. The chosen IDs are saved in session->chosen_f_teids. */
+static enum osmo_pfcp_cause up_session_choose_f_teid(struct up_session *session, struct osmo_pfcp_ie_f_teid *dst,
+ bool choose_id_present, uint8_t choose_id)
+{
+ struct up_endpoint *up_ep = session->up_peer->up_endpoint;
+ struct chosen_f_teid *chosen = NULL;
+
+ if (choose_id_present)
+ chosen = chosen_f_teid_find(&session->chosen_f_teids, choose_id);
+ if (chosen) {
+ /* Re-use a previous F-TEID */
+ *dst = chosen->f_teid;
+ } else {
+ /* Choose a new F-TEID */
+ *dst = (struct osmo_pfcp_ie_f_teid){
+ .fixed = {
+ .teid = up_endpoint_next_teid(up_ep),
+ },
+ };
+ if (dst->fixed.teid == 0) {
+ LOGPFSML(session->fi, LOGL_ERROR, "Failed to allocate an unused TEID\n");
+ return OSMO_PFCP_CAUSE_PFCP_ENTITY_IN_CONGESTION;
+ }
+ LOGPFSML(session->fi, LOGL_INFO, "Allocated new local TEID 0x%x\n", dst->fixed.teid);
+
+ if (osmo_pfcp_ip_addrs_set(&dst->fixed.ip_addr, &up_ep->pfcp_ep->cfg.local_addr)) {
+ LOGPFSML(session->fi, LOGL_ERROR, "Invalid local address in pfcp_endpoint cfg\n");
+ return OSMO_PFCP_CAUSE_PFCP_ENTITY_IN_CONGESTION;
+ }
+ /* Save this choice */
+ if (choose_id_present) {
+ chosen = talloc(session, struct chosen_f_teid);
+ *chosen = (struct chosen_f_teid){
+ .f_teid = *dst,
+ .choose_id = choose_id,
+ };
+ llist_add_tail(&chosen->entry, &session->chosen_f_teids);
+ }
+ }
+ return OSMO_PFCP_CAUSE_REQUEST_ACCEPTED;
+}
+
+static struct far *far_create(struct up_session *session,
+ const struct osmo_pfcp_ie_create_far *create_far)
+{
+ struct far *far = talloc(session, struct far);
+ *far = (struct far){
+ .session = session,
+ .desc = *create_far,
+ };
+ llist_add_tail(&far->entry, &session->fars);
+
+ return far;
+}
+
+static struct far *far_find(struct up_session *session, uint32_t far_id)
+{
+ struct far *far;
+ llist_for_each_entry(far, &session->fars, entry) {
+ if (far->desc.far_id == far_id)
+ return far;
+ }
+ return NULL;
+}
+
+static void far_upd(struct far *far, const struct osmo_pfcp_ie_upd_far *upd)
+{
+ if (upd->apply_action_present)
+ far->desc.apply_action = upd->apply_action;
+ if (upd->upd_forw_params_present) {
+ const struct osmo_pfcp_ie_upd_forw_params *u = &upd->upd_forw_params;
+ struct osmo_pfcp_ie_forw_params *p = &far->desc.forw_params;
+ if (u->destination_iface_present)
+ p->destination_iface = u->destination_iface;
+ if (u->network_inst_present) {
+ p->network_inst = p->network_inst;
+ p->network_inst_present = true;
+ }
+ if (u->outer_header_creation_present) {
+ p->outer_header_creation = p->outer_header_creation;
+ p->outer_header_creation_present = true;
+ }
+ if (u->linked_te_id_present) {
+ p->linked_te_id = p->linked_te_id;
+ p->linked_te_id_present = true;
+ }
+ if (u->destination_iface_type_present) {
+ p->destination_iface_type = p->destination_iface_type;
+ p->destination_iface_type_present = true;
+ }
+ }
+}
+
+struct pdr;
+static void pdr_classify(struct pdr *pdr);
+
+static void far_del(struct far *far)
+{
+ struct pdr *pdr;
+ llist_for_each_entry(pdr, &far->session->pdrs, entry) {
+ if (pdr->far == far) {
+ pdr->far = NULL;
+ pdr_classify(pdr);
+ }
+ }
+
+ llist_del(&far->entry);
+ talloc_free(far);
+}
+
+static int far_to_str_buf(char *buf, size_t len, const struct far *far)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = len };
+ const struct osmo_pfcp_ie_create_far *f = &far->desc;
+
+ OSMO_STRBUF_PRINTF(sb, "FAR-%u{", f->far_id);
+ OSMO_STRBUF_APPEND(sb, osmo_pfcp_bits_to_str_buf, f->apply_action.bits, osmo_pfcp_apply_action_strs);
+ if (f->forw_params_present) {
+ OSMO_STRBUF_PRINTF(sb, " dst:%s", osmo_pfcp_dest_iface_str(f->forw_params.destination_iface));
+ if (f->forw_params.outer_header_creation_present) {
+ OSMO_STRBUF_PRINTF(sb, " encaps-");
+ OSMO_STRBUF_APPEND(sb, osmo_pfcp_bits_to_str_buf,
+ f->forw_params.outer_header_creation.desc_bits,
+ osmo_pfcp_outer_header_creation_strs);
+ if (f->forw_params.outer_header_creation.teid_present)
+ OSMO_STRBUF_PRINTF(sb, " TEID-0x%x", f->forw_params.outer_header_creation.teid);
+ }
+ }
+ OSMO_STRBUF_PRINTF(sb, "}");
+ return sb.chars_needed;
+}
+
+#if 0
+static char *far_to_str_c(void *ctx, const struct far *far)
+{
+ OSMO_NAME_C_IMPL(ctx, 64, "ERROR", far_to_str_buf, far)
+}
+#endif
+
+int pdr_to_str_buf(char *buf, size_t buflen, const struct pdr *pdr)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ const struct osmo_pfcp_ie_create_pdr *d = &pdr->desc;
+
+ OSMO_STRBUF_PRINTF(sb, "PDR-%u{src:%s", d->pdr_id, osmo_pfcp_source_iface_str(d->pdi.source_iface));
+ if (pdr->desc.pdi.ue_ip_address_present) {
+ if (pdr->desc.pdi.ue_ip_address.ip_addr.v4_present) {
+ OSMO_STRBUF_PRINTF(sb, " %s",
+ pdr->desc.pdi.ue_ip_address.ip_is_destination ? "dst:" : "src:");
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &pdr->desc.pdi.ue_ip_address.ip_addr.v4);
+ }
+ if (pdr->desc.pdi.ue_ip_address.ip_addr.v6_present) {
+ OSMO_STRBUF_PRINTF(sb, " ");
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &pdr->desc.pdi.ue_ip_address.ip_addr.v6);
+ }
+ }
+ if (pdr->local_f_teid) {
+ OSMO_STRBUF_PRINTF(sb, " ");
+ OSMO_STRBUF_APPEND(sb, osmo_pfcp_ie_f_teid_to_str_buf, pdr->local_f_teid);
+ }
+ if (d->outer_header_removal_present)
+ OSMO_STRBUF_PRINTF(sb, " decaps-%s",
+ osmo_pfcp_outer_header_removal_desc_str(d->outer_header_removal.desc));
+ OSMO_STRBUF_PRINTF(sb, "}");
+
+ if (pdr->far) {
+ OSMO_STRBUF_PRINTF(sb, " --> ");
+ OSMO_STRBUF_APPEND(sb, far_to_str_buf, pdr->far);
+ }
+
+ return sb.chars_needed;
+}
+
+char *pdr_to_str_c(void *ctx, const struct pdr *pdr)
+{
+ OSMO_NAME_C_IMPL(ctx, 128, "ERROR", pdr_to_str_buf, pdr)
+}
+
+static struct pdr *pdr_find(struct up_session *session, uint16_t pdr_id)
+{
+ struct pdr *pdr;
+ llist_for_each_entry(pdr, &session->pdrs, entry) {
+ if (pdr->desc.pdr_id == pdr_id)
+ return pdr;
+ }
+ return NULL;
+}
+
+static void pdr_del(struct pdr *pdr)
+{
+ llist_del(&pdr->entry);
+ talloc_free(pdr);
+}
+
+static void pdr_set_far(struct pdr *pdr, struct far *far)
+{
+ pdr->far = far;
+}
+
+static struct pdr *pdr_create(struct up_session *session,
+ const struct osmo_pfcp_ie_create_pdr *create_pdr,
+ enum osmo_pfcp_cause *cause,
+ bool *offending_ie_present,
+ enum osmo_pfcp_iei *offending_ie,
+ struct osmo_pfcp_ie_created_pdr created_pdr[],
+ unsigned int *created_pdr_count,
+ size_t created_pdr_maxcount)
+{
+ struct pdr *pdr = NULL;
+
+ /* Is there still room in the response for 'Created PDR' IEs? */
+ if (*created_pdr_count >= created_pdr_maxcount) {
+ *cause = OSMO_PFCP_CAUSE_NO_RESOURCES_AVAILABLE;
+ goto nack_resp;
+ }
+
+ pdr = talloc(session, struct pdr);
+ *pdr = (struct pdr){
+ .session = session,
+ .desc = *create_pdr,
+ };
+ llist_add_tail(&pdr->entry, &session->pdrs);
+
+ if (pdr->desc.far_id_present) {
+ struct far *far = far_find(session, pdr->desc.far_id);
+ if (!far) {
+ LOGPFSML(session->fi, LOGL_ERROR, "PDR-%u requests FAR-%u, but there is no such FAR\n",
+ pdr->desc.pdr_id, pdr->desc.far_id);
+ *cause = OSMO_PFCP_CAUSE_REQUEST_REJECTED;
+ *offending_ie_present = true;
+ *offending_ie = OSMO_PFCP_IEI_FAR_ID;
+ goto nack_resp;
+ }
+ pdr_set_far(pdr, far);
+ } else if (pdr->desc.activate_predefined_rules_present) {
+ LOGPFSML(session->fi, LOGL_ERROR,
+ "Predefined Rules feature not implemented: PDR-%u requests predefined rule '%s'\n",
+ pdr->desc.pdr_id, pdr->desc.activate_predefined_rules.str);
+ *cause = OSMO_PFCP_CAUSE_RULE_CREATION_MOD_FAILURE;
+ *offending_ie_present = true;
+ *offending_ie = OSMO_PFCP_IEI_ACTIVATE_PREDEFINED_RULES;
+ goto nack_resp;
+ } else {
+ LOGPFSML(session->fi, LOGL_ERROR, "No FAR defined for PDR-%u\n", pdr->desc.pdr_id);
+ *cause = OSMO_PFCP_CAUSE_CONDITIONAL_IE_MISSING;
+ *offending_ie_present = true;
+ *offending_ie = OSMO_PFCP_IEI_FAR_ID;
+ goto nack_resp;
+ }
+
+ /* Figure out the TEID and IP address for this PDR */
+ if (pdr->desc.pdi.local_f_teid_present) {
+ if (pdr->desc.pdi.local_f_teid.choose_flag) {
+ /* CHOOSE = 1: we need to pick our own local F-TEID */
+ struct osmo_pfcp_ie_f_teid local_f_teid;
+ *cause = up_session_choose_f_teid(session, &local_f_teid,
+ pdr->desc.pdi.local_f_teid.choose.choose_id_present,
+ pdr->desc.pdi.local_f_teid.choose.choose_id);
+ if (*cause != OSMO_PFCP_CAUSE_REQUEST_ACCEPTED) {
+ *offending_ie = OSMO_PFCP_IEI_F_TEID;
+ *offending_ie_present = true;
+ goto nack_resp;
+ }
+ pdr->_local_f_teid_buf = local_f_teid;
+ pdr->local_f_teid = &pdr->_local_f_teid_buf;
+ } else {
+ /* CHOOSE = 0: just use the provided TEID and IP addr */
+ pdr->local_f_teid = &pdr->desc.pdi.local_f_teid;
+ if (!(pdr->local_f_teid->fixed.ip_addr.v4_present
+ || pdr->local_f_teid->fixed.ip_addr.v6_present)) {
+ LOGPFSML(session->fi, LOGL_ERROR,
+ "peer requested to choose an F-TEID, but neither IPv4 nor IPv6 is selected"
+ " in the incoming Create PDR IE for PDR-%u.\n",
+ pdr->desc.pdr_id);
+ *cause = OSMO_PFCP_CAUSE_RULE_CREATION_MOD_FAILURE;
+ *offending_ie = OSMO_PFCP_IEI_F_TEID;
+ *offending_ie_present = true;
+ goto nack_resp;
+ }
+ }
+ }
+
+ if (pdr->local_f_teid) {
+ created_pdr[*created_pdr_count] = (struct osmo_pfcp_ie_created_pdr){
+ .pdr_id = pdr->desc.pdr_id,
+ .local_f_teid_present = true,
+ .local_f_teid = *pdr->local_f_teid,
+ };
+ } else {
+ created_pdr[*created_pdr_count] = (struct osmo_pfcp_ie_created_pdr){
+ .pdr_id = pdr->desc.pdr_id,
+ };
+ }
+ (*created_pdr_count)++;
+
+ LOGPFSML(session->fi, LOGL_INFO, "New %s\n", pdr_to_str_c(OTC_SELECT, pdr));
+
+ return pdr;
+
+nack_resp:
+ if (pdr)
+ pdr_del(pdr);
+ if (!*offending_ie_present) {
+ *offending_ie = OSMO_PFCP_IEI_CREATE_PDR;
+ *offending_ie_present = true;
+ }
+ return NULL;
+}
+
+static struct pdr *pdr_upd(struct pdr *pdr,
+ const struct osmo_pfcp_ie_upd_pdr *update_pdr,
+ enum osmo_pfcp_cause *cause,
+ bool *offending_ie_present,
+ enum osmo_pfcp_iei *offending_ie,
+ struct osmo_pfcp_ie_updated_pdr updated_pdr[],
+ unsigned int *updated_pdr_count,
+ size_t updated_pdr_maxcount)
+{
+ struct up_session *session = pdr->session;
+
+ /* Is there still room in the response for 'Updated PDR' IEs? */
+ if (*updated_pdr_count >= updated_pdr_maxcount) {
+ *cause = OSMO_PFCP_CAUSE_NO_RESOURCES_AVAILABLE;
+ goto nack_resp;
+ }
+
+ if (update_pdr->outer_header_removal_present) {
+ pdr->desc.outer_header_removal = update_pdr->outer_header_removal;
+ pdr->desc.outer_header_removal_present = true;
+ }
+ if (update_pdr->pdi_present)
+ pdr->desc.pdi = update_pdr->pdi;
+
+ if (update_pdr->far_id_present) {
+ struct far *far = far_find(session, update_pdr->far_id);
+ if (!far) {
+ LOGPFSML(session->fi, LOGL_ERROR, "PDR-%u requests FAR-%u, but there is no such FAR\n",
+ pdr->desc.pdr_id, pdr->desc.far_id);
+ *cause = OSMO_PFCP_CAUSE_REQUEST_REJECTED;
+ *offending_ie_present = true;
+ *offending_ie = OSMO_PFCP_IEI_FAR_ID;
+ goto nack_resp;
+ }
+ pdr_set_far(pdr, far);
+ }
+
+ if (pdr->local_f_teid) {
+ updated_pdr[*updated_pdr_count] = (struct osmo_pfcp_ie_updated_pdr){
+ .pdr_id = pdr->desc.pdr_id,
+ .local_f_teid_present = true,
+ .local_f_teid = *pdr->local_f_teid,
+ };
+ } else {
+ updated_pdr[*updated_pdr_count] = (struct osmo_pfcp_ie_updated_pdr){
+ .pdr_id = pdr->desc.pdr_id,
+ };
+ }
+ (*updated_pdr_count)++;
+
+ pdr_classify(pdr);
+ LOGPFSML(session->fi, LOGL_INFO, "Updated %s\n", pdr_to_str_c(OTC_SELECT, pdr));
+
+ return pdr;
+
+nack_resp:
+ if (pdr)
+ pdr_del(pdr);
+ if (!*offending_ie_present) {
+ *offending_ie = OSMO_PFCP_IEI_UPD_PDR;
+ *offending_ie_present = true;
+ }
+ return NULL;
+}
+
+char *up_session_gtp_status(struct up_session *session)
+{
+ struct pdr *pdr;
+ struct far *far;
+ int active_pdrs = 0;
+ int total_pdrs = 0;
+ int active_fars = 0;
+ int total_fars = 0;
+
+ llist_for_each_entry(pdr, &session->pdrs, entry) {
+ if (pdr->active)
+ active_pdrs++;
+ total_pdrs++;
+ }
+
+ llist_for_each_entry(far, &session->fars, entry) {
+ if (far->active)
+ active_fars++;
+ total_fars++;
+ }
+
+ return talloc_asprintf(OTC_SELECT, "PDR-active:%d/%d FAR-active:%d/%d GTP-active:%u",
+ active_pdrs, total_pdrs, active_fars, total_fars,
+ llist_count(&session->active_gtp_actions));
+}
+
+static void up_session_est(struct up_session *session, struct osmo_pfcp_msg *m)
+{
+ struct osmo_fsm_inst *fi = session->fi;
+ struct up_peer *peer = session->up_peer;
+ struct osmo_pfcp_msg_session_est_req *req = &m->ies.session_est_req;
+ struct osmo_pfcp_msg_session_est_resp *resp;
+ struct osmo_pfcp_msg *tx;
+ int i;
+ int rc;
+
+ tx = up_session_init_tx(session, m, OSMO_PFCP_MSGT_SESSION_EST_RESP);
+ resp = &tx->ies.session_est_resp;
+
+ for (i = 0; i < req->create_far_count; i++) {
+ struct far *far = far_create(session, &req->create_far[i]);
+ if (!far)
+ goto nack_response;
+ }
+
+ for (i = 0; i < req->create_pdr_count; i++) {
+ struct pdr *pdr = pdr_create(session, &req->create_pdr[i],
+ &resp->cause,
+ &resp->offending_ie_present, &resp->offending_ie,
+ resp->created_pdr, &resp->created_pdr_count,
+ ARRAY_SIZE(resp->created_pdr));
+ if (!pdr)
+ goto nack_response;
+ }
+
+ resp->cause = up_session_setup_gtp(session);
+ if (resp->cause != OSMO_PFCP_CAUSE_REQUEST_ACCEPTED)
+ goto nack_response;
+
+ /* Success, send ACK */
+ osmo_pfcp_ie_f_seid_set(&resp->up_f_seid, session->up_seid, &peer->up_endpoint->pfcp_ep->cfg.local_addr);
+ resp->up_f_seid_present = true;
+
+ rc = osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, tx);
+ if (rc)
+ up_session_fsm_state_chg(UP_SESSION_ST_WAIT_USE_COUNT);
+ up_session_fsm_state_chg(UP_SESSION_ST_ESTABLISHED);
+ return;
+
+nack_response:
+ resp->created_pdr_count = 0;
+ osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, tx);
+ up_session_fsm_state_chg(UP_SESSION_ST_WAIT_USE_COUNT);
+}
+
+static void up_session_mod(struct up_session *session, struct osmo_pfcp_msg *m)
+{
+ struct osmo_fsm_inst *fi = session->fi;
+ struct up_peer *peer = session->up_peer;
+ struct osmo_pfcp_msg_session_mod_req *req = &m->ies.session_mod_req;
+ struct osmo_pfcp_msg_session_mod_resp *resp;
+ struct osmo_pfcp_msg *tx;
+ int i;
+
+ tx = up_session_init_tx(session, m, OSMO_PFCP_MSGT_SESSION_MOD_RESP);
+ resp = &tx->ies.session_mod_resp;
+
+ for (i = 0; i < req->remove_far_count; i++) {
+ uint32_t far_id = req->remove_far[i].far_id;
+ struct far *far = far_find(session, far_id);
+ if (!far) {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Cannot remove, does not exist: FAR-%u\n", far_id);
+ continue;
+ }
+ far_del(far);
+ }
+
+ for (i = 0; i < req->remove_pdr_count; i++) {
+ uint16_t pdr_id = req->remove_pdr[i].pdr_id;
+ struct pdr *pdr = pdr_find(session, pdr_id);
+ if (!pdr) {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Cannot remove, does not exist: PDR-%u\n", pdr_id);
+ continue;
+ }
+ pdr_del(pdr);
+ }
+
+ for (i = 0; i < req->create_far_count; i++) {
+ struct far *far = far_create(session, &req->create_far[i]);
+ if (!far)
+ goto nack_response;
+ }
+
+ for (i = 0; i < req->create_pdr_count; i++) {
+ struct pdr *pdr = pdr_create(session, &req->create_pdr[i],
+ &resp->cause,
+ &resp->offending_ie_present, &resp->offending_ie,
+ resp->created_pdr, &resp->created_pdr_count,
+ ARRAY_SIZE(resp->created_pdr));
+ if (!pdr)
+ goto nack_response;
+ }
+
+ for (i = 0; i < req->upd_far_count; i++) {
+ uint32_t far_id = req->upd_far[i].far_id;
+ struct far *far = far_find(session, req->upd_far[i].far_id);
+ if (!far) {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Cannot update, does not exist: FAR-%u\n", far_id);
+ goto nack_response;
+ }
+ far_upd(far, &req->upd_far[i]);
+ }
+
+ for (i = 0; i < req->upd_pdr_count; i++) {
+ uint16_t pdr_id = req->upd_pdr[i].pdr_id;
+ struct pdr *pdr = pdr_find(session, req->upd_pdr[i].pdr_id);
+ if (!pdr) {
+ OSMO_LOG_PFCP_MSG(m, LOGL_ERROR, "Cannot update, does not exist: PDR-%u\n", pdr_id);
+ goto nack_response;
+ }
+ pdr_upd(pdr, &req->upd_pdr[i],
+ &resp->cause,
+ &resp->offending_ie_present, &resp->offending_ie,
+ resp->updated_pdr, &resp->updated_pdr_count,
+ ARRAY_SIZE(resp->updated_pdr));
+ }
+
+ resp->cause = up_session_setup_gtp(session);
+ if (resp->cause != OSMO_PFCP_CAUSE_REQUEST_ACCEPTED)
+ goto nack_response;
+
+ /* Success, send ACK */
+ if (osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, tx))
+ up_session_fsm_state_chg(UP_SESSION_ST_WAIT_USE_COUNT);
+
+ LOGPFSML(fi, LOGL_NOTICE, "Session modified: %s\n", up_session_gtp_status(session));
+ return;
+
+nack_response:
+ resp->created_pdr_count = 0;
+ osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, tx);
+ up_session_fsm_state_chg(UP_SESSION_ST_WAIT_USE_COUNT);
+}
+
+static void up_session_del(struct up_session *session, struct osmo_pfcp_msg *m)
+{
+ struct osmo_fsm_inst *fi = session->fi;
+ struct up_peer *peer = session->up_peer;
+ struct osmo_pfcp_msg *tx;
+
+ tx = up_session_init_tx(session, m, OSMO_PFCP_MSGT_SESSION_DEL_RESP);
+ tx->ies.session_del_resp = (struct osmo_pfcp_msg_session_del_resp){
+ .cause = OSMO_PFCP_CAUSE_REQUEST_ACCEPTED
+ };
+ osmo_pfcp_endpoint_tx(peer->up_endpoint->pfcp_ep, tx);
+ up_session_fsm_state_chg(UP_SESSION_ST_WAIT_USE_COUNT);
+}
+
+static void up_session_init_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ struct up_session *up_session = fi->priv;
+
+ switch (event) {
+
+ case UP_SESSION_EV_RX_SESSION_EST_REQ:
+ up_session_est(up_session, data);
+ break;
+
+ case UP_SESSION_EV_RX_SESSION_DEL_REQ:
+ up_session_del(up_session, data);
+ break;
+
+ case UP_SESSION_EV_USE_COUNT_ZERO:
+ /* ignore */
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void up_session_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ struct up_session *session = fi->priv;
+ LOGPFSML(fi, LOGL_NOTICE, "Session established: %s %s\n", up_session_to_str_c(OTC_SELECT, session),
+ up_session_gtp_status(session));
+}
+
+static void up_session_established_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ struct up_session *up_session = fi->priv;
+
+ switch (event) {
+
+ case UP_SESSION_EV_RX_SESSION_MOD_REQ:
+ up_session_mod(up_session, data);
+ break;
+
+ case UP_SESSION_EV_RX_SESSION_DEL_REQ:
+ up_session_del(up_session, data);
+ break;
+
+ case UP_SESSION_EV_USE_COUNT_ZERO:
+ /* ignore */
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void up_session_established_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
+{
+ struct up_session *session = fi->priv;
+ struct up_gtp_action *a;
+ LOGPFSML(fi, LOGL_NOTICE, "Session releasing: %s %s\n", up_session_to_str_c(OTC_SELECT, session), up_session_gtp_status(session));
+
+ /* Shut down all active GTP rules */
+ while ((a = llist_first_entry_or_null(&session->active_gtp_actions, struct up_gtp_action, entry))) {
+ up_gtp_action_disable(a);
+ llist_del(&a->entry);
+ talloc_free(a);
+ }
+}
+
+
+static void up_session_clear_pdr_far(struct up_session *session);
+static void drop_gtp_actions(struct up_session *session);
+
+int up_session_discard(struct up_session *session)
+{
+ struct osmo_fsm_inst *fi = session->fi;
+ if (fi->state == UP_SESSION_ST_WAIT_USE_COUNT)
+ return 0;
+ up_session_fsm_state_chg(UP_SESSION_ST_WAIT_USE_COUNT);
+ return 1;
+}
+
+static void up_session_wait_use_count_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ struct up_session *session = fi->priv;
+
+ drop_gtp_actions(session);
+ up_session_clear_pdr_far(session);
+
+ if (!osmo_use_count_total(&session->use_count))
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
+}
+
+static void up_session_wait_use_count_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+
+ case UP_SESSION_EV_USE_COUNT_ZERO:
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void up_session_clear_pdr_far(struct up_session *session)
+{
+ struct pdr *pdr;
+ struct far *far;
+ while ((pdr = llist_first_entry_or_null(&session->pdrs, struct pdr, entry)))
+ pdr_del(pdr);
+ while ((far = llist_first_entry_or_null(&session->fars, struct far, entry)))
+ far_del(far);
+}
+
+static void drop_gtp_actions(struct up_session *session);
+
+static void up_session_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
+{
+ struct up_session *session = fi->priv;
+
+ drop_gtp_actions(session);
+
+ up_session_clear_pdr_far(session);
+
+ hash_del(&session->node_by_up_seid);
+ hash_del(&session->node_by_cp_seid);
+}
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state up_session_fsm_states[] = {
+ [UP_SESSION_ST_INIT] = {
+ .name = "INIT",
+ .in_event_mask = 0
+ | S(UP_SESSION_EV_RX_SESSION_EST_REQ)
+ | S(UP_SESSION_EV_RX_SESSION_DEL_REQ)
+ | S(UP_SESSION_EV_USE_COUNT_ZERO)
+ ,
+ .out_state_mask = 0
+ | S(UP_SESSION_ST_ESTABLISHED)
+ | S(UP_SESSION_ST_WAIT_USE_COUNT)
+ ,
+ .action = up_session_init_action,
+ },
+ [UP_SESSION_ST_ESTABLISHED] = {
+ .name = "ESTABLISHED",
+ .in_event_mask = 0
+ | S(UP_SESSION_EV_RX_SESSION_MOD_REQ)
+ | S(UP_SESSION_EV_RX_SESSION_DEL_REQ)
+ | S(UP_SESSION_EV_USE_COUNT_ZERO)
+ ,
+ .out_state_mask = 0
+ | S(UP_SESSION_ST_WAIT_USE_COUNT)
+ ,
+ .onenter = up_session_established_onenter,
+ .action = up_session_established_action,
+ .onleave = up_session_established_onleave,
+ },
+ [UP_SESSION_ST_WAIT_USE_COUNT] = {
+ .name = "WAIT_USE_COUNT",
+ .in_event_mask = 0
+ | S(UP_SESSION_EV_USE_COUNT_ZERO)
+ ,
+ .onenter = up_session_wait_use_count_onenter,
+ .action = up_session_wait_use_count_action,
+ },
+};
+
+static struct osmo_fsm up_session_fsm = {
+ .name = "up_session",
+ .states = up_session_fsm_states,
+ .num_states = ARRAY_SIZE(up_session_fsm_states),
+ .log_subsys = DSESSION,
+ .event_names = up_session_fsm_event_names,
+ .timer_cb = up_session_fsm_timer_cb,
+ .cleanup = up_session_fsm_cleanup,
+};
+
+static __attribute__((constructor)) void up_session_fsm_register(void)
+{
+ OSMO_ASSERT(osmo_fsm_register(&up_session_fsm) == 0);
+}
+
+static int up_session_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
+{
+ struct up_session *session = e->use_count->talloc_object;
+ int32_t total;
+ int level;
+
+ if (!e->use)
+ return -EINVAL;
+
+ total = osmo_use_count_total(&session->use_count);
+
+ if (total == 0
+ || (total == 1 && old_use_count == 0 && e->count == 1))
+ level = LOGL_INFO;
+ else
+ level = LOGL_DEBUG;
+
+ LOGPFSMSLSRC(session->fi, DREF, level, file, line,
+ "%s %s: now used by %s\n",
+ (e->count - old_use_count) > 0? "+" : "-", e->use,
+ osmo_use_count_to_str_c(OTC_SELECT, &session->use_count));
+
+ if (e->count < 0)
+ return -ERANGE;
+
+ if (total == 0)
+ osmo_fsm_inst_dispatch(session->fi, UP_SESSION_EV_USE_COUNT_ZERO, NULL);
+ return 0;
+}
+
+static void up_session_update_id(struct up_session *session)
+{
+ osmo_fsm_inst_update_id_f_sanitize(session->fi, '-', "%s-0x%" PRIx64,
+ up_peer_remote_addr_str(session->up_peer),
+ session->up_seid);
+ LOGPFSML(session->fi, LOGL_DEBUG, "Updated id\n");
+}
+
+static inline uint64_t up_session_key(uint64_t cp_seid, uint64_t up_seid)
+{
+ return cp_seid + up_seid;
+}
+
+static struct up_session *up_session_add(struct up_peer *peer, const struct osmo_pfcp_ie_f_seid *cp_f_seid)
+{
+ struct up_session *session;
+ uint64_t up_seid = up_endpoint_next_seid(peer->up_endpoint);
+
+ if (!up_seid)
+ return NULL;
+
+ struct osmo_fsm_inst *fi = osmo_fsm_inst_alloc_child(&up_session_fsm, peer->fi, UP_PEER_EV_SESSION_TERM);
+ OSMO_ASSERT(fi);
+
+ session = talloc(fi, struct up_session);
+ OSMO_ASSERT(session);
+ fi->priv = session;
+
+ *session = (struct up_session) {
+ .fi = fi,
+ .up_peer = peer,
+ .cp_f_seid = *cp_f_seid,
+ .up_seid = up_seid,
+ .use_count = {
+ .talloc_object = session,
+ .use_cb = up_session_use_cb,
+ },
+ };
+ INIT_LLIST_HEAD(&session->pdrs);
+ INIT_LLIST_HEAD(&session->fars);
+ INIT_LLIST_HEAD(&session->chosen_f_teids);
+ INIT_LLIST_HEAD(&session->active_gtp_actions);
+ osmo_use_count_make_static_entries(&session->use_count, session->use_count_buf, ARRAY_SIZE(session->use_count_buf));
+ LOGPFSML(session->fi, LOGL_INFO, "Allocated new UP-SEID: 0x%" PRIx64 "\n", session->up_seid);
+ up_session_update_id(session);
+
+ hash_add(peer->sessions_by_up_seid, &session->node_by_up_seid, session->up_seid);
+ hash_add(peer->sessions_by_cp_seid, &session->node_by_cp_seid, session->cp_f_seid.seid);
+ return session;
+}
+
+struct up_session *up_session_find_or_add(struct up_peer *peer, const struct osmo_pfcp_ie_f_seid *cp_f_seid,
+ const struct osmo_pfcp_ie_f_seid *up_f_seid)
+{
+ struct up_session *session;
+ if (cp_f_seid)
+ session = up_session_find_by_cp_f_seid(peer, cp_f_seid);
+ else if (up_f_seid)
+ session = up_session_find_by_up_seid(peer, up_f_seid->seid);
+ else
+ return NULL;
+ if (session)
+ return session;
+
+ return up_session_add(peer, cp_f_seid);
+}
+
+struct up_session *up_session_find_by_up_seid(struct up_peer *peer, uint64_t up_seid)
+{
+ struct up_session *session;
+ hash_for_each_possible(peer->sessions_by_up_seid, session, node_by_up_seid, up_seid) {
+ if (up_seid == session->up_seid)
+ return session;
+ }
+ return NULL;
+}
+
+struct up_session *up_session_find_by_cp_f_seid(struct up_peer *peer, const struct osmo_pfcp_ie_f_seid *cp_f_seid)
+{
+ struct up_session *session;
+ hash_for_each_possible(peer->sessions_by_cp_seid, session, node_by_cp_seid, cp_f_seid->seid) {
+ if (osmo_pfcp_ie_f_seid_cmp(&session->cp_f_seid, cp_f_seid) == 0)
+ return session;
+ }
+ return NULL;
+}
+
+struct up_session *up_session_find_by_local_teid(struct up_peer *peer, uint32_t teid)
+{
+ struct up_session *session;
+ int bkt;
+ hash_for_each(peer->sessions_by_up_seid, bkt, session, node_by_up_seid) {
+ struct pdr *pdr;
+ llist_for_each_entry(pdr, &session->pdrs, entry) {
+ if (!pdr->local_f_teid)
+ continue;
+ if (pdr->local_f_teid->fixed.teid == teid)
+ return session;
+ }
+ }
+ return NULL;
+}
+
+static bool action_is_forw(const struct osmo_pfcp_ie_apply_action *aa)
+{
+ return osmo_pfcp_bits_get(aa->bits, OSMO_PFCP_APPLY_ACTION_FORW)
+ && !osmo_pfcp_bits_get(aa->bits, OSMO_PFCP_APPLY_ACTION_DROP);
+}
+
+static void pdr_classify(struct pdr *pdr)
+{
+ pdr->rx_decaps = false;
+ pdr->forw_encaps = false;
+ pdr->forw_to_core = false;
+ pdr->forw_from_core = false;
+ if (!pdr->far)
+ return;
+
+ pdr->rx_decaps = (pdr->desc.outer_header_removal_present
+ && pdr->desc.outer_header_removal.desc == OSMO_PFCP_OUTER_HEADER_REMOVAL_GTP_U_UDP_IPV4);
+ pdr->forw_encaps = (pdr->far->desc.forw_params_present
+ && pdr->far->desc.forw_params.outer_header_creation_present);
+
+ if (!action_is_forw(&pdr->far->desc.apply_action))
+ return;
+
+ pdr->forw_to_core = (pdr->desc.pdi.source_iface == OSMO_PFCP_SOURCE_IFACE_ACCESS
+ && pdr->far->desc.forw_params.destination_iface == OSMO_PFCP_DEST_IFACE_CORE);
+
+ pdr->forw_from_core = (pdr->desc.pdi.source_iface == OSMO_PFCP_SOURCE_IFACE_CORE
+ && pdr->far->desc.forw_params.destination_iface == OSMO_PFCP_DEST_IFACE_ACCESS);
+}
+
+void pdr_reverse_set(struct pdr *a, struct pdr *b)
+{
+ if (a)
+ a->reverse_pdr = b;
+ if (b)
+ b->reverse_pdr = a;
+}
+
+void pdr_reverse_unset(struct pdr *pdr)
+{
+ if (!pdr->reverse_pdr)
+ return;
+ pdr->reverse_pdr->reverse_pdr = NULL;
+ pdr->reverse_pdr = NULL;
+}
+
+static void log_inactive_pdr_set(struct pdr *pdr, const char *desc, const struct pdr *pdr_to_str)
+{
+ struct pdr *rpdr = pdr->reverse_pdr;
+ pdr_to_str = pdr_to_str ? : pdr;
+ osmo_talloc_replace_string_fmt(pdr, &pdr->inactive_reason, "%s (in PDR-%u %s)", desc,
+ pdr_to_str->desc.pdr_id,
+ osmo_pfcp_source_iface_str(pdr_to_str->desc.pdi.source_iface));
+ if (rpdr)
+ osmo_talloc_replace_string_fmt(rpdr, &rpdr->inactive_reason, "%s (in PDR-%u %s)", desc,
+ pdr_to_str->desc.pdr_id,
+ osmo_pfcp_source_iface_str(pdr_to_str->desc.pdi.source_iface));
+ if (rpdr)
+ LOGPFSML(pdr->session->fi, LOGL_INFO, "Inactive set: PDR-%u + PDR-%u: %s: %s\n",
+ pdr->desc.pdr_id, rpdr->desc.pdr_id, desc, pdr_to_str_c(OTC_SELECT, pdr_to_str));
+ else
+ LOGPFSML(pdr->session->fi, LOGL_INFO, "Inactive: PDR-%u: %s: %s\n",
+ pdr->desc.pdr_id, desc, pdr_to_str_c(OTC_SELECT, pdr_to_str));
+}
+
+/* A GTP tunnel on Access side, plain IP on Core side.
+ * The given PDR must have an outer-header-removal and a local F-TEID.
+ * Its reverse-PDR must have a UE address flagged as "Destination" IP addr.
+ * Its reverse-PDR's FAR must have an outer-header creation with a remote TEID.
+ */
+static void add_gtp_action_endecaps(void *ctx, struct llist_head *dst, struct pdr *pdr)
+{
+ struct up_session *session = pdr->session;
+ struct up_gtp_action *a;
+ struct pdr *rpdr;
+ struct far *rfar;
+ struct osmo_pfcp_ie_forw_params *rfar_forw;
+
+ OSMO_ASSERT(pdr->far);
+ OSMO_ASSERT(pdr->reverse_pdr);
+ OSMO_ASSERT(pdr->reverse_pdr->far);
+
+ /* To decaps, we need to have a local TEID assigned for which to receive GTP packets. */
+ if (!pdr->local_f_teid || pdr->local_f_teid->choose_flag) {
+ log_inactive_pdr_set(pdr, "missing local TEID", pdr);
+ return;
+ }
+
+ /* To encaps, we need to have a remote TEID assigned to send out in GTP packets, and we need to know where to
+ * send GTP to. */
+ rpdr = pdr->reverse_pdr;
+ rfar = rpdr->far;
+ rfar_forw = &rfar->desc.forw_params;
+ if (!rfar->desc.forw_params_present) {
+ log_inactive_pdr_set(pdr, "missing FAR Forwarding Parameters", rpdr);
+ return;
+ }
+ if (!rfar_forw->outer_header_creation_present) {
+ log_inactive_pdr_set(pdr, "missing FAR Outer Header Creation", rpdr);
+ return;
+ }
+ if (!rfar_forw->outer_header_creation.teid_present) {
+ log_inactive_pdr_set(pdr, "missing TEID in FAR Outer Header Creation", rpdr);
+ return;
+ }
+ if (!rfar_forw->outer_header_creation.ip_addr.v4_present) {
+ log_inactive_pdr_set(pdr, "missing IPv4 in FAR Outer Header Creation", rpdr);
+ return;
+ }
+
+ /* To receive packets to be encapsulated, we need to know the assigned IP address for the UE, which receives the
+ * IP packets that should be placed into GTP. */
+ if (!rpdr->desc.pdi.ue_ip_address_present) {
+ log_inactive_pdr_set(pdr, "missing UE IP Address in PDI", rpdr);
+ return;
+ }
+ if (!rpdr->desc.pdi.ue_ip_address.ip_addr.v4_present) {
+ log_inactive_pdr_set(pdr, "UE IP Address in PDI is not IPv4", rpdr);
+ return;
+ }
+ if (!rpdr->desc.pdi.ue_ip_address.ip_is_destination) {
+ log_inactive_pdr_set(pdr, "UE IP Address in PDI is not flagged as destination", rpdr);
+ return;
+ }
+
+ pdr->active = true;
+ pdr->far->active = true;
+ rpdr->active = true;
+ rpdr->far->active = true;
+ LOGPFSML(session->fi, LOGL_DEBUG, "Active PDR set: %s\n", pdr_to_str_c(OTC_SELECT, pdr));
+ LOGPFSML(session->fi, LOGL_DEBUG, "Active PDR set: + %s\n", pdr_to_str_c(OTC_SELECT, rpdr));
+
+ talloc_free(pdr->inactive_reason);
+ pdr->inactive_reason = NULL;
+ talloc_free(rpdr->inactive_reason);
+ rpdr->inactive_reason = NULL;
+
+ a = talloc(ctx, struct up_gtp_action);
+ OSMO_ASSERT(a);
+ *a = (struct up_gtp_action){
+ .session = session,
+ .pdr_core = pdr->desc.pdr_id,
+ .pdr_access = rpdr->desc.pdr_id,
+ .kind = UP_GTP_U_ENDECAPS,
+ .endecaps = {
+ .local_teid = pdr->local_f_teid->fixed.teid,
+ .remote_teid = rfar_forw->outer_header_creation.teid,
+ .gtp_remote_addr = rfar_forw->outer_header_creation.ip_addr.v4,
+ .ue_addr = rpdr->desc.pdi.ue_ip_address.ip_addr.v4,
+ },
+ };
+
+ llist_add_tail(&a->entry, dst);
+}
+
+static void add_gtp_action_forw(void *ctx, struct llist_head *dst, struct pdr *pdr)
+{
+ /* TODO implement GTP forwarding with TEID translation */
+}
+
+/* Analyse all PDRs and FARs and find configurations that match either a GTP encaps/decaps or a GTP forward rule. Add to
+ * list dst, containing struct up_gtp_action instances allocated from ctx. */
+static enum osmo_pfcp_cause find_gtp_actions(void *ctx, struct llist_head *dst, struct up_session *session)
+{
+ struct far *far;
+ struct pdr *pdr;
+
+ llist_for_each_entry(far, &session->fars, entry) {
+ far->active = false;
+ }
+
+ llist_for_each_entry(pdr, &session->pdrs, entry) {
+ pdr->reverse_pdr = NULL;
+ pdr_classify(pdr);
+ pdr->active = false;
+ }
+
+ llist_for_each_entry(pdr, &session->pdrs, entry) {
+ struct pdr *other;
+
+ /* Already paired up in an earlier iteration? */
+ if (pdr->reverse_pdr)
+ continue;
+
+ /* In this outer loop, only follow the forw_to_core directed PDRs, in the inner loop find the matching
+ * forw_from_core PDR. */
+ if (!pdr->forw_to_core)
+ continue;
+
+ /* If a required TEID is not known, we cannot pair this PDR up */
+ if (pdr->rx_decaps && !pdr->local_f_teid)
+ continue;
+
+ /* Try to find a matching PDR+FAR that points in the reverse direction. */
+ llist_for_each_entry(other, &session->pdrs, entry) {
+ /* Already paired up in an earlier iteration? */
+ if (other->reverse_pdr)
+ continue;
+
+ /* Looking for a PDR facing the other way */
+ if (!other->forw_from_core)
+ continue;
+ /* GTP header-ness must match, in reverse. */
+ if (pdr->rx_decaps != other->forw_encaps
+ || pdr->forw_encaps != other->rx_decaps)
+ continue;
+
+ /* TEID: when adding a GTP header, we only know the remote side TEID sent out.
+ * When removing a GTP header, we only know the local TEID that the remote side sends here.
+ * So can't match up TEIDs. */
+
+ /* Just match these up, simply based on direction and encaps/decaps match */
+ pdr_reverse_set(pdr, other);
+ }
+ }
+
+ /* Iterate again to create the GTP actions (and debug log what is going on) */
+ llist_for_each_entry(pdr, &session->pdrs, entry) {
+ if (!pdr->reverse_pdr) {
+ LOGPFSML(session->fi, LOGL_INFO, "Inactive PDR: no matching reverse PDR for: %s\n", pdr_to_str_c(OTC_SELECT, pdr));
+ continue;
+ }
+
+ /* Iterate in direction to-Core, where pdr->reverse_pdr will be the from-Core counterpart. */
+ if (!pdr->forw_to_core)
+ continue;
+
+ if (pdr->rx_decaps && !pdr->forw_encaps)
+ add_gtp_action_endecaps(ctx, dst, pdr);
+ else if (pdr->rx_decaps && pdr->forw_encaps)
+ add_gtp_action_forw(ctx, dst, pdr);
+ else {
+ /* log the details of both PDRs in two separate log lines */
+ log_inactive_pdr_set(pdr, "not implemented", pdr);
+ log_inactive_pdr_set(pdr, "not implemented", pdr->reverse_pdr);
+ }
+
+ }
+
+ return OSMO_PFCP_CAUSE_REQUEST_ACCEPTED;
+}
+
+static enum osmo_pfcp_cause setup_gtp_actions(struct up_session *session, struct llist_head *want)
+{
+ struct up_gtp_action *a, *a_safe;
+ struct up_gtp_action *w, *w_safe;
+
+ LOGPFSML(session->fi, LOGL_DEBUG, "GTP actions: %u previously active; want active: %u\n",
+ llist_count(&session->active_gtp_actions), llist_count(want));
+
+ llist_for_each_entry(w, want, entry) {
+ LOGPFSML(session->fi, LOGL_DEBUG, "want: %s\n", up_gtp_action_to_str_c(OTC_SELECT, w));
+ w->handle = NULL;
+ }
+
+ /* Match up the wanted GTP tunnels with the already active ones */
+ llist_for_each_entry(a, &session->active_gtp_actions, entry) {
+ LOGPFSML(session->fi, LOGL_DEBUG, "active: %s\n", up_gtp_action_to_str_c(OTC_SELECT, a));
+ a->handle = NULL;
+ llist_for_each_entry(w, want, entry) {
+ /* Already matched up? */
+ if (w->handle)
+ continue;
+ if (up_gtp_action_cmp(a, w))
+ continue;
+ /* Found a match, mark. */
+ a->handle = w;
+ w->handle = a;
+ }
+ }
+
+ /* At this point, all matching entries in session->active_gtp_actions and the 'want' list have a handle != NULL.
+ * If handle == NULL in active_gtp_actions, it means it has no match in the wanted list and is to be torn down.
+ * If handle == NULL in 'want', it means it has no match in the active list and should be created. */
+
+ /* Shut down all active GTP rules that no longer appear in the session setup. */
+ llist_for_each_entry_safe(a, a_safe, &session->active_gtp_actions, entry) {
+ if (a->handle)
+ continue;
+
+ LOGPFSML(session->fi, LOGL_DEBUG, "disabling: %s\n", up_gtp_action_to_str_c(OTC_SELECT, a));
+ up_gtp_action_disable(a);
+ llist_del(&a->entry);
+ talloc_free(a);
+ }
+
+ /* Set up all GTP tunnels requested in the session setup, but not active yet */
+ llist_for_each_entry_safe(w, w_safe, want, entry) {
+ if (w->handle)
+ continue;
+
+ LOGPFSML(session->fi, LOGL_DEBUG, "enabling: %s\n", up_gtp_action_to_str_c(OTC_SELECT, w));
+
+ /* If enabling fails, don't add to the active list. Error logging is done in up_gtp_action_enable(). */
+ if (up_gtp_action_enable(w))
+ continue;
+
+ /* Successfully activated, move the entry from 'want' to 'active_gtp_actions' */
+ llist_del(&w->entry);
+ talloc_steal(session, w);
+ llist_add_tail(&w->entry, &session->active_gtp_actions);
+ }
+
+ return OSMO_PFCP_CAUSE_REQUEST_ACCEPTED;
+}
+
+static void drop_gtp_actions(struct up_session *session)
+{
+ struct llist_head empty;
+ INIT_LLIST_HEAD(&empty);
+ setup_gtp_actions(session, &empty);
+}
+
+/* Check whether the Packet Detection and Forwarding Action Rules amount to an encaps/decaps of GTP or a GTP forwarding,
+ * or none of the two. */
+static enum osmo_pfcp_cause up_session_setup_gtp(struct up_session *session)
+{
+ enum osmo_pfcp_cause cause;
+ struct llist_head want_gtp_actions;
+ INIT_LLIST_HEAD(&want_gtp_actions);
+ cause = find_gtp_actions(OTC_SELECT, &want_gtp_actions, session);
+ if (cause != OSMO_PFCP_CAUSE_REQUEST_ACCEPTED)
+ return cause;
+ cause = setup_gtp_actions(session, &want_gtp_actions);
+ return cause;
+}
+
+bool up_session_is_active(struct up_session *session)
+{
+ return session && (session->fi->state == UP_SESSION_ST_ESTABLISHED) && !llist_empty(&session->active_gtp_actions);
+}
+
+bool up_session_is_fully_active(struct up_session *session, int *active_p, int *inactive_p)
+{
+ struct pdr *pdr;
+ int inactive = 0;
+ int active = 0;
+ llist_for_each_entry(pdr, &session->pdrs, entry) {
+ if (pdr->active)
+ active++;
+ else
+ inactive++;
+ }
+ if (!up_session_is_active(session)) {
+ inactive += active;
+ active = 0;
+ }
+ if (active_p)
+ *active_p = active;
+ if (inactive_p)
+ *inactive_p = inactive;
+ return active && !inactive;
+}
+
+int up_session_to_str_buf(char *buf, size_t buflen, struct up_session *session)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ if (!session) {
+ OSMO_STRBUF_PRINTF(sb, "session=null");
+ return sb.chars_needed;
+ }
+ OSMO_STRBUF_PRINTF(sb, "peer:%s SEID-r:0x%"PRIx64" SEID-l:0x%"PRIx64" state:%s",
+ up_peer_remote_addr_str(session->up_peer),
+ session->cp_f_seid.seid, session->up_seid,
+ osmo_fsm_inst_state_name(session->fi));
+ return sb.chars_needed;
+}
+
+char *up_session_to_str_c(void *ctx, struct up_session *session)
+{
+ OSMO_NAME_C_IMPL(ctx, 128, "ERROR", up_session_to_str_buf, session)
+}
diff --git a/src/osmo-upf/upf.c b/src/osmo-upf/upf.c
index 6463e32..234368a 100644
--- a/src/osmo-upf/upf.c
+++ b/src/osmo-upf/upf.c
@@ -23,13 +23,69 @@
#include <osmocom/core/utils.h>
#include <osmocom/core/talloc.h>
+#include <osmocom/core/sockaddr_str.h>
+
+#include <osmocom/pfcp/pfcp_endpoint.h>
#include <osmocom/upf/upf.h>
+#include <osmocom/upf/up_endpoint.h>
+#include <osmocom/upf/upf_gtp.h>
struct g_upf *g_upf = NULL;
+struct osmo_tdef_group g_upf_tdef_groups[] = {
+ { "pfcp", "PFCP endpoint timers", osmo_pfcp_tdefs, },
+ {}
+};
+
void g_upf_alloc(void *ctx)
{
OSMO_ASSERT(g_upf == NULL);
g_upf = talloc_zero(ctx, struct g_upf);
+
+ *g_upf = (struct g_upf){
+ .pfcp = {
+ .vty_cfg = {
+ .local_addr = talloc_strdup(g_upf, UPF_PFCP_LISTEN_DEFAULT),
+ .local_port = OSMO_PFCP_PORT,
+ },
+ },
+ };
+
+ INIT_LLIST_HEAD(&g_upf->gtp.vty_cfg.devs);
+ INIT_LLIST_HEAD(&g_upf->gtp.devs);
+}
+
+int upf_pfcp_listen()
+{
+ struct osmo_sockaddr_str local_addr_str;
+ struct osmo_sockaddr local_addr;
+
+ OSMO_ASSERT(g_upf);
+ OSMO_ASSERT(g_upf->pfcp.ep == NULL);
+
+ /* Translate address string from VTY config to osmo_sockaddr: first read into osmo_sockaddr_str, then write to
+ * osmo_sockaddr. */
+ osmo_sockaddr_str_from_str(&local_addr_str, g_upf->pfcp.vty_cfg.local_addr, g_upf->pfcp.vty_cfg.local_port);
+ osmo_sockaddr_str_to_sockaddr(&local_addr_str, &local_addr.u.sas);
+ LOGP(DLPFCP, LOGL_NOTICE, "PFCP: Listening on %s\n", osmo_sockaddr_to_str_c(OTC_SELECT, &local_addr));
+
+ g_upf->pfcp.ep = up_endpoint_init(g_upf, &local_addr);;
+ if (!g_upf->pfcp.ep) {
+ fprintf(stderr, "Failed to allocate PFCP endpoint.\n");
+ return -1;
+ }
+ return 0;
+}
+
+int upf_gtp_devs_open()
+{
+ struct gtp_vty_cfg *c = &g_upf->gtp.vty_cfg;
+ struct gtp_vty_cfg_dev *d;
+
+ llist_for_each_entry(d, &c->devs, entry) {
+ if (upf_gtp_dev_open(d->dev_name, d->create, d->local_addr, false, false))
+ return -1;
+ }
+ return 0;
}
diff --git a/src/osmo-upf/upf_gtp.c b/src/osmo-upf/upf_gtp.c
new file mode 100644
index 0000000..84d74d1
--- /dev/null
+++ b/src/osmo-upf/upf_gtp.c
@@ -0,0 +1,458 @@
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdbool.h>
+#include <errno.h>
+#include <string.h>
+#include <net/if.h>
+#include <linux/gtp.h>
+
+#include <libgtpnl/gtpnl.h>
+#include <libgtpnl/gtp.h>
+
+#include <osmocom/core/socket.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/sockaddr_str.h>
+
+#include <osmocom/upf/upf.h>
+#include <osmocom/upf/upf_gtp.h>
+
+#define LOG_GTP_TUN(TUN, LEVEL, FMT, ARGS...) \
+ LOGP(DGTP, LEVEL, "%s: " FMT, upf_gtp_tun_to_str_c(OTC_SELECT, (TUN)), ##ARGS)
+
+int upf_gtp_dev_to_str_buf(char *buf, size_t buflen, const struct upf_gtp_dev *dev)
+{
+ uint16_t v0_port;
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ OSMO_STRBUF_PRINTF(sb, "%s", dev->name ? : "null");
+ if (dev->name && dev->ifidx)
+ OSMO_STRBUF_PRINTF(sb, " [%u]", dev->ifidx);
+ if (dev->sgsn_mode)
+ OSMO_STRBUF_PRINTF(sb, " (SGSN)");
+ OSMO_STRBUF_PRINTF(sb, " ");
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &dev->gtpv1.local_addr);
+ v0_port = osmo_sockaddr_port(&dev->gtpv0.local_addr.u.sa);
+ if (dev->gtpv0.enabled && v0_port)
+ OSMO_STRBUF_PRINTF(sb, "/%u", v0_port);
+ return sb.chars_needed;
+}
+
+char *upf_gtp_dev_to_str_c(void *ctx, const struct upf_gtp_dev *dev)
+{
+ OSMO_NAME_C_IMPL(ctx, 64, "ERROR", upf_gtp_dev_to_str_buf, dev)
+}
+
+struct upf_gtp_dev *upf_gtp_dev_find_by_name(const char *name)
+{
+ struct upf_gtp_dev *dev;
+ llist_for_each_entry(dev, &g_upf->gtp.devs, entry) {
+ if (!strcmp(name, dev->name))
+ return dev;
+ }
+ return NULL;
+}
+
+struct upf_gtp_dev *upf_gtp_dev_first()
+{
+ return llist_first_entry_or_null(&g_upf->gtp.devs, struct upf_gtp_dev, entry);
+}
+
+/* Tell the kernel to remove the GTP device. Called implicitly by talloc_free() (see upf_gtp_dev_destruct()). */
+static int upf_gtp_dev_delete(struct upf_gtp_dev *dev)
+{
+ int rc;
+ if (!dev->name)
+ return 0;
+ rc = gtp_dev_destroy(dev->name);
+ if (rc < 0) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "Error while deleting device: %s\n", strerror(errno));
+ return rc;
+ }
+ LOG_GTP_DEV(dev, LOGL_NOTICE, "Deleted GTP device\n");
+ dev->name = NULL;
+ return 0;
+}
+
+static int upf_gtp_dev_destruct(struct upf_gtp_dev *dev);
+
+static struct upf_gtp_dev *upf_gtp_dev_alloc(const char *name, const char *local_addr)
+{
+ struct upf_gtp_dev *dev = upf_gtp_dev_find_by_name(name);
+ struct osmo_sockaddr_str addr_conv;
+ local_addr = local_addr ? : "0.0.0.0";
+ if (dev) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "Device already exists. Cannot create %s %s\n", name, local_addr);
+ return NULL;
+ }
+ dev = talloc(g_upf, struct upf_gtp_dev);
+ *dev = (struct upf_gtp_dev){
+ .name = talloc_strdup(dev, name),
+ .gtpv0.ofd.fd = -1,
+ .gtpv1.ofd.fd = -1,
+ };
+ INIT_LLIST_HEAD(&dev->tunnels);
+
+ osmo_sockaddr_str_from_str(&addr_conv, local_addr, PORT_GTP0_U);
+
+ osmo_sockaddr_str_to_sockaddr(&addr_conv, &dev->gtpv0.local_addr.u.sas);
+
+ addr_conv.port = PORT_GTP1_U;
+ osmo_sockaddr_str_to_sockaddr(&addr_conv, &dev->gtpv1.local_addr.u.sas);
+
+ /* Need to add to list before setting up the destructor. A talloc_free() does automagically remove from the
+ * list. */
+ llist_add(&dev->entry, &g_upf->gtp.devs);
+
+ talloc_set_destructor(dev, upf_gtp_dev_destruct);
+
+ return dev;
+}
+
+static int dev_resolve_ifidx(struct upf_gtp_dev *dev)
+{
+ int rc;
+
+ dev->ifidx = if_nametoindex(dev->name);
+ if (dev->ifidx == 0) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "No such device: '%s'\n", dev->name);
+ talloc_free(dev);
+ return -EIO;
+ }
+ /* Let's try something to see if talking to the device works. */
+ errno = 0;
+ rc = gtp_list_tunnel(g_upf->gtp.genl_id, g_upf->gtp.nl);
+ if (errno)
+ rc = -errno;
+ else if (rc)
+ rc = -EINVAL;
+ if (rc) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "Failed to open GTP device: %s\n", strerror(-rc));
+ talloc_free(dev);
+ return rc;
+ }
+
+ LOG_GTP_DEV(dev, LOGL_NOTICE, "GTP device ready (ifidx=%u)\n", dev->ifidx);
+ return 0;
+}
+
+int upf_gtp_dev_open(const char *name, bool create_gtp_dev, const char *local_addr, bool listen_for_gtpv0, bool sgsn_mode)
+{
+ struct osmo_sockaddr any = {
+ .u.sin = {
+ .sin_family = AF_INET,
+ .sin_port = 0,
+ .sin_addr = {
+ .s_addr = INADDR_ANY,
+ },
+ },
+ };
+ int rc;
+ struct upf_gtp_dev *dev = upf_gtp_dev_alloc(name, local_addr);
+ if (!dev)
+ return -EIO;
+
+ dev->sgsn_mode = sgsn_mode;
+
+ if (listen_for_gtpv0) {
+ rc = osmo_sock_init_osa_ofd(&dev->gtpv0.ofd, SOCK_DGRAM, 0, &dev->gtpv0.local_addr, &any,
+ OSMO_SOCK_F_BIND);
+ if (rc < 0) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "Cannot bind GTPv0 on %s (rc=%d)\n",
+ osmo_sockaddr_to_str_c(OTC_SELECT, &dev->gtpv0.local_addr), rc);
+ talloc_free(dev);
+ return -EIO;
+ }
+ dev->gtpv0.enabled = true;
+ LOG_GTP_DEV(dev, LOGL_DEBUG, "GTPv0 bound\n");
+ }
+
+ /* GTPv1 */
+ rc = osmo_sock_init_osa_ofd(&dev->gtpv1.ofd, SOCK_DGRAM, 0, &dev->gtpv1.local_addr, &any,
+ OSMO_SOCK_F_BIND);
+ if (rc < 0) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "Cannot bind GTPv1 (rc=%d)\n", rc);
+ talloc_free(dev);
+ return -EIO;
+ }
+ LOG_GTP_DEV(dev, LOGL_DEBUG, "GTPv1 bound\n");
+
+ if (create_gtp_dev) {
+ int gtp0_fd = listen_for_gtpv0 ? dev->gtpv0.ofd.fd : -1;
+ int gtp1_fd = dev->gtpv1.ofd.fd;
+ if (dev->sgsn_mode)
+ rc = gtp_dev_create_sgsn(-1, dev->name, gtp0_fd, gtp1_fd);
+ else
+ rc = gtp_dev_create(-1, dev->name, gtp0_fd, gtp1_fd);
+ if (rc < 0) {
+ LOG_GTP_DEV(dev, LOGL_ERROR, "Cannot create GTP device: rc=%d\n", rc);
+ /* name = NULL: signal to the destructor that it does not need to delete the device */
+ dev->name = NULL;
+ talloc_free(dev);
+ return -EIO;
+ }
+
+ LOG_GTP_DEV(dev, LOGL_NOTICE, "created GTP device\n");
+ dev->created = true;
+ }
+
+ rc = dev_resolve_ifidx(dev);
+ if (rc) {
+ talloc_free(dev);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+void upf_gtp_devs_close()
+{
+ struct upf_gtp_dev *dev;
+ while ((dev = llist_first_entry_or_null(&g_upf->gtp.devs, struct upf_gtp_dev, entry)))
+ talloc_free(dev);
+}
+
+void upf_gtp_genl_close()
+{
+ if (!g_upf->gtp.nl)
+ return;
+ genl_socket_close(g_upf->gtp.nl);
+ g_upf->gtp.nl = NULL;
+ g_upf->gtp.genl_id = -1;
+
+ LOGP(DGTP, LOGL_NOTICE, "Closed mnl_socket\n");
+}
+
+/* Open an MNL socket which allows to create and remove GTP devices (requires CAP_NET_ADMIN). */
+int upf_gtp_genl_open()
+{
+ if (g_upf->gtp.nl && g_upf->gtp.genl_id >= 0)
+ return 0;
+
+ if (g_upf->gtp.nl)
+ upf_gtp_genl_close();
+
+ g_upf->gtp.nl = genl_socket_open();
+ if (!g_upf->gtp.nl) {
+ LOGP(DGTP, LOGL_ERROR, "Cannot open mnl_socket: %s\n", strerror(errno));
+ return -EIO;
+ }
+
+ g_upf->gtp.genl_id = genl_lookup_family(g_upf->gtp.nl, "gtp");
+ if (g_upf->gtp.genl_id < 0) {
+ LOGP(DGTP, LOGL_ERROR, "genl family 'gtp' not found\n");
+ return -ENOTSUP;
+ }
+
+ LOGP(DGTP, LOGL_NOTICE, "Opened mnl_socket\n");
+ return 0;
+}
+
+struct upf_gtp_tun {
+ struct llist_head entry;
+
+ struct upf_gtp_dev *dev;
+ struct upf_gtp_tun_desc desc;
+ bool active;
+};
+
+static int upf_gtp_tun_to_str_buf(char *buf, size_t buflen, const struct upf_gtp_tun *tun)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ OSMO_STRBUF_PRINTF(sb, "%s:tun{TEID=l:0x%x,r:0x%x UE=", tun->dev->name, tun->desc.local_teid,
+ tun->desc.remote_teid);
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &tun->desc.ue_addr);
+ OSMO_STRBUF_PRINTF(sb, " GTP-dst=");
+ OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &tun->desc.gtp_remote_addr);
+ OSMO_STRBUF_PRINTF(sb, "}");
+ return sb.chars_needed;
+}
+
+static char *upf_gtp_tun_to_str_c(void *ctx, const struct upf_gtp_tun *tun)
+{
+ OSMO_NAME_C_IMPL(ctx, 64, "ERROR", upf_gtp_tun_to_str_buf, tun)
+}
+
+static int upf_gtp_tun_deactivate(struct upf_gtp_tun *tun);
+
+static int upf_gtp_tun_destruct(struct upf_gtp_tun *tun)
+{
+ if (tun->active)
+ upf_gtp_tun_deactivate(tun);
+ llist_del(&tun->entry);
+ return 0;
+}
+
+static struct upf_gtp_tun *upf_gtp_tun_alloc(struct upf_gtp_dev *dev, const struct upf_gtp_tun_desc *desc)
+{
+ struct upf_gtp_tun *tun = talloc(dev, struct upf_gtp_tun);
+ OSMO_ASSERT(tun);
+ *tun = (struct upf_gtp_tun){
+ .dev = dev,
+ .desc = *desc,
+ };
+ llist_add(&tun->entry, &dev->tunnels);
+ talloc_set_destructor(tun, upf_gtp_tun_destruct);
+ return tun;
+}
+
+static struct gtp_tunnel *upf_gtp_tun_to_gtp_tunnel(struct upf_gtp_tun *tun)
+{
+ struct gtp_tunnel *t;
+
+ if (tun->desc.ue_addr.u.sas.ss_family != AF_INET || tun->desc.gtp_remote_addr.u.sas.ss_family != AF_INET) {
+ LOG_GTP_TUN(tun, LOGL_ERROR, "Only capabale of IPv4\n");
+ return NULL;
+ }
+
+ t = gtp_tunnel_alloc();
+ OSMO_ASSERT(t);
+ gtp_tunnel_set_ifidx(t, tun->dev->ifidx);
+ gtp_tunnel_set_version(t, GTP_V1);
+ gtp_tunnel_set_i_tei(t, tun->desc.local_teid);
+ gtp_tunnel_set_o_tei(t, tun->desc.remote_teid);
+ gtp_tunnel_set_ms_ip4(t, &tun->desc.ue_addr.u.sin.sin_addr);
+ gtp_tunnel_set_sgsn_ip4(t, &tun->desc.gtp_remote_addr.u.sin.sin_addr);
+ return t;
+}
+
+int upf_gtp_tun_activate(struct upf_gtp_tun *tun)
+{
+ int rc;
+ struct gtp_tunnel *t;
+
+ if (tun->active)
+ return -EALREADY;
+
+ t = upf_gtp_tun_to_gtp_tunnel(tun);
+ if (!t)
+ return -ENOTSUP;
+
+ errno = 0;
+ rc = gtp_add_tunnel(g_upf->gtp.genl_id, g_upf->gtp.nl, t);
+ if (errno) {
+ rc = -errno;
+ } else if (rc) {
+ rc = -EINVAL;
+ } else {
+ tun->active = true;
+ }
+
+ gtp_tunnel_free(t);
+ return rc;
+}
+
+static struct upf_gtp_tun *upf_gtp_dev_tunnel_find(struct upf_gtp_dev *dev, const struct upf_gtp_tun_desc *tun_desc)
+{
+ struct upf_gtp_tun *tun;
+ llist_for_each_entry(tun, &dev->tunnels, entry) {
+ if (upf_gtp_tun_desc_cmp(tun_desc, &tun->desc))
+ continue;
+ return tun;
+ }
+ return NULL;
+}
+
+int upf_gtp_dev_tunnel_add(struct upf_gtp_dev *dev, const struct upf_gtp_tun_desc *tun_desc)
+{
+ struct upf_gtp_tun *tun;
+ tun = upf_gtp_dev_tunnel_find(dev, tun_desc);
+ if (!tun)
+ tun = upf_gtp_tun_alloc(dev, tun_desc);
+ if (tun->active)
+ return 0;
+ return upf_gtp_tun_activate(tun);
+}
+
+int upf_gtp_dev_tunnel_del(struct upf_gtp_dev *dev, const struct upf_gtp_tun_desc *tun_desc)
+{
+ struct upf_gtp_tun *tun;
+ int rc;
+ tun = upf_gtp_dev_tunnel_find(dev, tun_desc);
+ if (!tun)
+ return 0;
+ if (tun->active) {
+ rc = upf_gtp_tun_deactivate(tun);
+ if (rc)
+ return rc;
+ }
+ talloc_free(tun);
+ return 0;
+}
+
+static int upf_gtp_tun_deactivate(struct upf_gtp_tun *tun)
+{
+ int rc;
+ struct gtp_tunnel *t;
+
+ if (!tun->active) {
+ LOG_GTP_TUN(tun, LOGL_ERROR, "Cannot deactivate, not active\n");
+ return -EINVAL;
+ }
+
+ t = upf_gtp_tun_to_gtp_tunnel(tun);
+ if (!t)
+ return -EINVAL;
+
+ rc = gtp_del_tunnel(g_upf->gtp.genl_id, g_upf->gtp.nl, t);
+ if (rc)
+ LOG_GTP_TUN(tun, LOGL_ERROR, "Failed to delete tunnel: %d %s\n", rc, strerror(rc));
+ else
+ tun->active = false;
+
+ gtp_tunnel_free(t);
+ return rc;
+}
+
+static int upf_gtp_dev_destruct(struct upf_gtp_dev *dev)
+{
+ struct upf_gtp_tun *t;
+ /* Destruct and clean up all active tunnels before deleting the device */
+ while ((t = llist_first_entry_or_null(&dev->tunnels, struct upf_gtp_tun, entry)))
+ talloc_free(t);
+ llist_del(&dev->entry);
+ osmo_fd_close(&dev->gtpv0.ofd);
+ osmo_fd_close(&dev->gtpv1.ofd);
+ if (dev->created)
+ upf_gtp_dev_delete(dev);
+ return 0;
+}
+
+int upf_gtp_tun_desc_cmp(const struct upf_gtp_tun_desc *a, const struct upf_gtp_tun_desc *b)
+{
+ if (a == b)
+ return 0;
+ if (!a)
+ return -1;
+ if (!b)
+ return 1;
+
+#define CMP_RET(MEMB) do { \
+ int _cmp = OSMO_CMP(a->MEMB, b->MEMB); \
+ if (_cmp) \
+ return _cmp; \
+ } while (0)
+
+ CMP_RET(local_teid);
+ CMP_RET(remote_teid);
+ return osmo_sockaddr_cmp(&a->gtp_remote_addr, &b->gtp_remote_addr);
+}
diff --git a/src/osmo-upf/upf_vty.c b/src/osmo-upf/upf_vty.c
new file mode 100644
index 0000000..73d86d1
--- /dev/null
+++ b/src/osmo-upf/upf_vty.c
@@ -0,0 +1,286 @@
+/* OsmoUpf interface to quagga VTY */
+/*
+ * (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved.
+ *
+ * Author: Neels Janosch Hofmeyr <nhofmeyr(a)sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/core/sockaddr_str.h>
+#include <osmocom/core/socket.h>
+
+#include <osmocom/pfcp/pfcp_endpoint.h>
+#include <osmocom/pfcp/pfcp_msg.h>
+
+#include <osmocom/vty/vty.h>
+#include <osmocom/vty/command.h>
+
+#include <osmocom/upf/upf.h>
+#include <osmocom/upf/upf_gtp.h>
+#include <osmocom/upf/up_endpoint.h>
+#include <osmocom/upf/up_peer.h>
+#include <osmocom/upf/up_session.h>
+#include <osmocom/upf/up_gtp_action.h>
+
+enum upf_vty_node {
+ PFCP_NODE = _LAST_OSMOVTY_NODE + 1,
+ GTP_NODE,
+};
+
+static struct cmd_node cfg_pfcp_node = {
+ PFCP_NODE,
+ "%s(config-pfcp)# ",
+ 1,
+};
+
+#define pfcp_vty (g_upf->pfcp.vty_cfg)
+#define gtp_vty (g_upf->gtp.vty_cfg)
+
+DEFUN(cfg_pfcp, cfg_pfcp_cmd,
+ "pfcp",
+ "Enter the PFCP configuration node\n")
+{
+ vty->node = PFCP_NODE;
+ return CMD_SUCCESS;
+}
+
+static int config_write_pfcp(struct vty *vty)
+{
+ vty_out(vty, "pfcp%s", VTY_NEWLINE);
+ if (strcmp(UPF_PFCP_LISTEN_DEFAULT, pfcp_vty.local_addr))
+ vty_out(vty, " local-addr %s%s", pfcp_vty.local_addr, VTY_NEWLINE);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_pfcp_local_addr, cfg_pfcp_local_addr_cmd,
+ "local-addr IP_ADDR",
+ "Set the local IP address to bind on for PFCP\n"
+ "IP address\n")
+{
+ osmo_talloc_replace_string(g_upf, &pfcp_vty.local_addr, argv[0]);
+ return CMD_SUCCESS;
+}
+
+static struct cmd_node cfg_gtp_node = {
+ GTP_NODE,
+ "%s(config-gtp)# ",
+ 1,
+};
+
+DEFUN(cfg_gtp, cfg_gtp_cmd,
+ "gtp",
+ "Enter the GTP configuration node\n")
+{
+ vty->node = GTP_NODE;
+ return CMD_SUCCESS;
+}
+
+static int config_write_gtp(struct vty *vty)
+{
+ struct gtp_vty_cfg_dev *d;
+ vty_out(vty, "gtp%s", VTY_NEWLINE);
+
+ llist_for_each_entry(d, >p_vty.devs, entry) {
+ if (d->create) {
+ vty_out(vty, " dev create %s", d->dev_name);
+ if (d->local_addr)
+ vty_out(vty, " %s", d->local_addr);
+ vty_out(vty, "%s", VTY_NEWLINE);
+ } else {
+ vty_out(vty, " dev use %s%s", d->dev_name, VTY_NEWLINE);
+ }
+ }
+ return CMD_SUCCESS;
+}
+
+#define DEV_STR "Configure the GTP device to use for encaps/decaps.\n"
+
+DEFUN(cfg_gtp_dev_create, cfg_gtp_dev_create_cmd,
+ "dev create DEVNAME [LISTEN_ADDR]",
+ DEV_STR
+ "create a new GTP device. Will listen on GTPv1 port " OSMO_STRINGIFY_VAL(PORT_GTP1_U)
+ " and GTPv0 port " OSMO_STRINGIFY_VAL(PORT_GTP0_U) " on the specified interface, or on ANY if LISTEN_ADDR is"
+ " omitted.\n"
+ "device name, e.g. 'apn0'\n"
+ "IPv4 or IPv6 address to listen on, omit for ANY\n")
+{
+ struct gtp_vty_cfg_dev *d = talloc_zero(g_upf, struct gtp_vty_cfg_dev);
+ d->create = true;
+ d->dev_name = talloc_strdup(d, argv[0]);
+ if (argc > 1)
+ d->local_addr = talloc_strdup(d, argv[1]);
+ llist_add(&d->entry, >p_vty.devs);
+ vty_out(vty, "Added GTP device %s (create new)%s", d->dev_name, VTY_NEWLINE);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_gtp_dev_use, cfg_gtp_dev_use_cmd,
+ "dev use DEVNAME",
+ DEV_STR
+ "use an existing GTP device, e.g. created by 'gtp-link'\n"
+ "device name, e.g. 'apn0'\n")
+{
+ struct gtp_vty_cfg_dev *d = talloc_zero(g_upf, struct gtp_vty_cfg_dev);
+ d->create = false;
+ d->dev_name = talloc_strdup(d, argv[0]);
+ llist_add(&d->entry, >p_vty.devs);
+ vty_out(vty, "Added GTP device %s (use existing)%s", d->dev_name, VTY_NEWLINE);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_gtp_dev_del, cfg_gtp_dev_del_cmd,
+ "dev delete DEVNAME",
+ DEV_STR
+ "Remove a GTP device from the configuration, and delete the device if it was created here.\n"
+ "device name, e.g. 'apn0'\n")
+{
+ const char *dev_name = argv[0];
+ struct gtp_vty_cfg_dev *d;
+ struct upf_gtp_dev *dev;
+
+ /* remove from VTY cfg */
+ llist_for_each_entry(d, >p_vty.devs, entry) {
+ if (strcmp(d->dev_name, dev_name))
+ continue;
+ llist_del(&d->entry);
+ break;
+ }
+
+ /* close device (and possibly delete from system, via talloc destructor) */
+ dev = upf_gtp_dev_find_by_name(dev_name);
+ if (dev)
+ talloc_free(dev);
+ return CMD_SUCCESS;
+}
+
+DEFUN(show_pdr, show_pdr_cmd,
+ "show pdr",
+ SHOW_STR
+ "List all sessions' PDR and FAR status\n")
+{
+ struct up_peer *peer;
+ int active_count = 0;
+ int inactive_count = 0;
+ llist_for_each_entry(peer, &g_upf->pfcp.ep->peers, entry) {
+ struct up_session *session;
+ int bkt;
+ hash_for_each(peer->sessions_by_up_seid, bkt, session, node_by_up_seid) {
+ struct pdr *pdr;
+ llist_for_each_entry(pdr, &session->pdrs, entry) {
+ if (!pdr->active) {
+ vty_out(vty, "%s: inactive: %s%s%s%s",
+ session->fi->id, pdr_to_str_c(OTC_SELECT, pdr),
+ pdr->inactive_reason ? ": " : "",
+ pdr->inactive_reason ? : "",
+ VTY_NEWLINE);
+ inactive_count++;
+ } else {
+ vty_out(vty, "%s: active: %s%s",
+ session->fi->id, pdr_to_str_c(OTC_SELECT, pdr),
+ VTY_NEWLINE);
+ active_count++;
+ }
+ }
+ }
+ }
+ vty_out(vty, "(%d of %d active)%s", active_count, active_count + inactive_count, VTY_NEWLINE);
+ return CMD_SUCCESS;
+}
+
+DEFUN(show_gtp, show_gtp_cmd,
+ "show gtp",
+ SHOW_STR
+ "Active GTP tunnels and forwardings\n")
+{
+ struct up_peer *peer;
+ int count = 0;
+
+ if (!upf_gtp_dev_first()) {
+ vty_out(vty, "No GTP device open%s", VTY_NEWLINE);
+ return CMD_SUCCESS;
+ }
+
+ llist_for_each_entry(peer, &g_upf->pfcp.ep->peers, entry) {
+ struct up_session *session;
+ int bkt;
+ hash_for_each(peer->sessions_by_up_seid, bkt, session, node_by_up_seid) {
+ struct up_gtp_action *a;
+ llist_for_each_entry(a, &session->active_gtp_actions, entry) {
+ vty_out(vty, "%s%s", up_gtp_action_to_str_c(OTC_SELECT, a), VTY_NEWLINE);
+ count++;
+ }
+ }
+ }
+ vty_out(vty, "(%d active)%s", count, VTY_NEWLINE);
+ return CMD_SUCCESS;
+}
+
+DEFUN(show_session, show_session_cmd,
+ "show session",
+ SHOW_STR
+ "PFCP Session status\n")
+{
+ struct up_peer *peer;
+ int inactive_count = 0;
+ int active_count = 0;
+ int fully_active_count = 0;
+
+ llist_for_each_entry(peer, &g_upf->pfcp.ep->peers, entry) {
+ struct up_session *session;
+ int bkt;
+ hash_for_each(peer->sessions_by_up_seid, bkt, session, node_by_up_seid) {
+ vty_out(vty, "%s %s%s",
+ up_session_to_str_c(OTC_SELECT, session),
+ up_session_gtp_status(session), VTY_NEWLINE);
+ if (up_session_is_active(session)) {
+ if (up_session_is_fully_active(session, NULL, NULL))
+ fully_active_count++;
+ else
+ active_count++;
+ } else {
+ inactive_count++;
+ }
+ }
+ }
+ vty_out(vty, "(%d fully-active + %d partially active + %d inactive)%s",
+ fully_active_count, active_count, inactive_count, VTY_NEWLINE);
+ return CMD_SUCCESS;
+}
+
+void upf_vty_init()
+{
+ OSMO_ASSERT(g_upf != NULL);
+
+ install_element_ve(&show_pdr_cmd);
+ install_element_ve(&show_gtp_cmd);
+ install_element_ve(&show_session_cmd);
+
+ install_node(&cfg_pfcp_node, config_write_pfcp);
+ install_element(CONFIG_NODE, &cfg_pfcp_cmd);
+
+ install_element(PFCP_NODE, &cfg_pfcp_local_addr_cmd);
+
+ install_node(&cfg_gtp_node, config_write_gtp);
+ install_element(CONFIG_NODE, &cfg_gtp_cmd);
+
+ install_element(GTP_NODE, &cfg_gtp_dev_create_cmd);
+ install_element(GTP_NODE, &cfg_gtp_dev_use_cmd);
+ install_element(GTP_NODE, &cfg_gtp_dev_del_cmd);
+}
+
diff --git a/tests/upf.vty b/tests/upf.vty
index 02ea1af..ffaf969 100644
--- a/tests/upf.vty
+++ b/tests/upf.vty
@@ -2,3 +2,34 @@
OsmoUPF# configure terminal
OsmoUPF(config)# show running-config
...
+
+OsmoUPF(config)# pfcp
+OsmoUPF(config-pfcp)# list
+...
+ local-addr IP_ADDR
+OsmoUPF(config-pfcp)# local-addr?
+ local-addr Set the local IP address to bind on for PFCP
+OsmoUPF(config-pfcp)# local-addr ?
+ IP_ADDR IP address
+OsmoUPF(config-pfcp)# exit
+
+OsmoUPF(config)# gtp
+OsmoUPF(config-gtp)# list
+...
+ dev create DEVNAME [LISTEN_ADDR]
+ dev use DEVNAME
+ dev delete DEVNAME
+
+OsmoUPF(config-gtp)# dev?
+ dev Configure the GTP device to use for encaps/decaps.
+OsmoUPF(config-gtp)# dev ?
+ create create a new GTP device. Will listen on GTPv1 port 2152 and GTPv0 port 3386 on the specified interface, or on ANY if LISTEN_ADDR is omitted.
+ use use an existing GTP device, e.g. created by 'gtp-link'
+ delete Remove a GTP device from the configuration, and delete the device if it was created here.
+OsmoUPF(config-gtp)# dev create ?
+ DEVNAME device name, e.g. 'apn0'
+OsmoUPF(config-gtp)# dev create foo ?
+ [LISTEN_ADDR] IPv4 or IPv6 address to listen on, omit for any
+OsmoUPF(config-gtp)# dev delete ?
+ DEVNAME device name, e.g. 'apn0'
+OsmoUPF(config-gtp)# exit
--
To view, visit https://gerrit.osmocom.org/c/osmo-upf/+/28308
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-upf
Gerrit-Branch: master
Gerrit-Change-Id: I745bcbde6859004c41ddbfd2558036bf9a2d1de2
Gerrit-Change-Number: 28308
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: newchange
1
0
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-upf/+/28307 )
Change subject: add libgtpnl dependency
......................................................................
add libgtpnl dependency
Related: SYS#5599
Change-Id: I9928be6f62f5a89d98bdac63428f7a046c95c855
---
M configure.ac
M contrib/osmo-upf.spec.in
M debian/control
3 files changed, 3 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-upf refs/changes/07/28307/1
diff --git a/configure.ac b/configure.ac
index fe41cc9..119aa6c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -44,6 +44,7 @@
PKG_CHECK_MODULES(LIBOSMOCTRL, libosmoctrl >= 1.5.0)
PKG_CHECK_MODULES(LIBOSMOGTLV, libosmo-gtlv >= 0.1.0)
PKG_CHECK_MODULES(LIBOSMOPFCP, libosmo-pfcp >= 0.1.0)
+PKG_CHECK_MODULES(LIBGTPNL, libgtpnl >= 1.2.0)
dnl checks for header files
AC_HEADER_STDC
diff --git a/contrib/osmo-upf.spec.in b/contrib/osmo-upf.spec.in
index 3ae6ac4..78e8aae 100644
--- a/contrib/osmo-upf.spec.in
+++ b/contrib/osmo-upf.spec.in
@@ -32,6 +32,7 @@
%if 0%{?suse_version}
BuildRequires: systemd-rpm-macros
%endif
+BuildRequires: pkgconfig(libgtpnl) >= 1.2.0
BuildRequires: pkgconfig(libosmocore) >= 1.6.0
BuildRequires: pkgconfig(libosmoctrl) >= 1.6.0
BuildRequires: pkgconfig(libosmovty) >= 1.6.0
diff --git a/debian/control b/debian/control
index 04b8224..cabcd19 100644
--- a/debian/control
+++ b/debian/control
@@ -12,6 +12,7 @@
pkg-config,
python3-minimal,
libtalloc-dev,
+ libgtpnl-dev (>= 1.2.0),
libosmocore-dev (>= 1.6.0),
osmo-gsm-manuals-dev (>= 1.2.0)
Standards-Version: 3.9.8
--
To view, visit https://gerrit.osmocom.org/c/osmo-upf/+/28307
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-upf
Gerrit-Branch: master
Gerrit-Change-Id: I9928be6f62f5a89d98bdac63428f7a046c95c855
Gerrit-Change-Number: 28307
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: newchange
1
0