aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-11-08 16:07:12 +0900
committerHarald Welte <laforge@gnumonks.org>2017-11-14 00:08:51 +0900
commit22e1573831571448b920d276ef62ebed355c7df9 (patch)
treeff621e0f346ca0c0db2fce3d7e12680f04c894f7
parentc85e89961a4e9dd6fd0b75ce17e9c948c360e4a1 (diff)
gtp-kernel: Make sure repeated calls to gtp_kernel_init() are safe
We have to factor out the "run once" code and make sure to really only run that once, while the per-device code remains in the gtp_kernel_init() function. Change-Id: Iba5bd71e4b725eef59fe4f233fbb965e396a06c3
-rw-r--r--ggsn/gtp-kernel.c40
1 files changed, 25 insertions, 15 deletions
diff --git a/ggsn/gtp-kernel.c b/ggsn/gtp-kernel.c
index e1553bd..8f538a2 100644
--- a/ggsn/gtp-kernel.c
+++ b/ggsn/gtp-kernel.c
@@ -53,11 +53,36 @@ static struct {
struct mnl_socket *nl;
} gtp_nl;
+static int gtp_kernel_init_once(void)
+{
+ /* only initialize once */
+ if (gtp_nl.nl)
+ return 0;
+
+ gtp_nl.nl = genl_socket_open();
+ if (gtp_nl.nl == NULL) {
+ SYS_ERR(DGGSN, LOGL_ERROR, 0, "cannot create genetlink socket\n");
+ return -1;
+ }
+ gtp_nl.genl_id = genl_lookup_family(gtp_nl.nl, "gtp");
+ if (gtp_nl.genl_id < 0) {
+ SYS_ERR(DGGSN, LOGL_ERROR, 0,
+ "cannot lookup GTP genetlink ID\n");
+ return -1;
+ }
+ SYS_ERR(DGGSN, LOGL_DEBUG, 0, "Initialized GTP kernel mode (genl ID is %d)\n", gtp_nl.genl_id);
+
+ return 0;
+}
+
int gtp_kernel_init(struct gsn_t *gsn, const char *devname, struct in46_prefix *prefix, const char *ipup)
{
struct in_addr net;
const char *net_arg;
+ if (!gtp_nl.nl)
+ gtp_kernel_init_once();
+
if (prefix->addr.len != 4) {
SYS_ERR(DGGSN, LOGL_ERROR, 0,
"we only support IPv4 in this path :/");
@@ -72,21 +97,6 @@ int gtp_kernel_init(struct gsn_t *gsn, const char *devname, struct in46_prefix *
return -1;
}
- gtp_nl.nl = genl_socket_open();
- if (gtp_nl.nl == NULL) {
- SYS_ERR(DGGSN, LOGL_ERROR, 0,
- "cannot create genetlink socket\n");
- return -1;
- }
- gtp_nl.genl_id = genl_lookup_family(gtp_nl.nl, "gtp");
- if (gtp_nl.genl_id < 0) {
- SYS_ERR(DGGSN, LOGL_ERROR, 0,
- "cannot lookup GTP genetlink ID\n");
- return -1;
- }
- SYS_ERR(DGGSN, LOGL_DEBUG, 0,
- "Using the GTP kernel mode (genl ID is %d)\n", gtp_nl.genl_id);
-
net_arg = in46p_ntoa(prefix);
DEBUGP(DGGSN, "Setting route to reach %s via %s\n", net_arg, devname);