summaryrefslogtreecommitdiffstats
path: root/src/host/layer23/src/common/apn.c
blob: 169e30e9a3aff959d9a6b2ae786a7c06673a2a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * (C) 2023 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
 *
 * All Rights Reserved
 *
 * 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.
 *
 */

#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>

#include <talloc.h>

#include <osmocom/bb/common/logging.h>
#include <osmocom/bb/common/apn.h>
#include <osmocom/bb/common/ms.h>
#include <osmocom/bb/common/l23_app.h>

struct osmobb_apn *apn_alloc(struct osmocom_ms *ms, const char *name)
{
	struct osmobb_apn *apn;
	apn = talloc_zero(ms, struct osmobb_apn);
	if (!apn)
		return NULL;

	talloc_set_name(apn, "apn_%s", name);
	apn->cfg.name = talloc_strdup(apn, name);
	apn->cfg.shutdown = true;
	apn->cfg.tx_gpdu_seq = true;

	apn->tun = osmo_tundev_alloc(apn, name);
	if (!apn->tun) {
		talloc_free(apn);
		return NULL;
	}
	osmo_tundev_set_priv_data(apn->tun, apn);

	apn->ms = ms;
	llist_add_tail(&apn->list, &ms->gprs.apn_list);
	return apn;
}

void apn_free(struct osmobb_apn *apn)
{
	llist_del(&apn->list);
	osmo_tundev_free(apn->tun);
	talloc_free(apn);
}

int apn_start(struct osmobb_apn *apn)
{
	struct l23_app_info *app_info = l23_app_info();
	int rc;

	if (apn->started)
		return 0;

	LOGPAPN(LOGL_INFO, apn, "Opening TUN device %s\n", apn->cfg.dev_name);
	/* Set TUN library callback. Must have been configured by the app: */
	OSMO_ASSERT(app_info && app_info->tun_data_ind_cb);
	osmo_tundev_set_data_ind_cb(apn->tun, app_info->tun_data_ind_cb);
	osmo_tundev_set_dev_name(apn->tun, apn->cfg.dev_name);
	osmo_tundev_set_netns_name(apn->tun, apn->cfg.dev_netns_name);

	rc = osmo_tundev_open(apn->tun);
	if (rc < 0) {
		LOGPAPN(LOGL_ERROR, apn, "Failed to configure tun device\n");
		return -1;
	}

	LOGPAPN(LOGL_INFO, apn, "Opened TUN device %s\n", osmo_tundev_get_dev_name(apn->tun));

	/* TODO: set IP addresses on the tun device once we receive them from GGSN. See
	   osmo-ggsn.git's apn_start() */

	LOGPAPN(LOGL_NOTICE, apn, "Successfully started\n");
	apn->started = true;
	return 0;
}

int apn_stop(struct osmobb_apn *apn)
{
	LOGPAPN(LOGL_NOTICE, apn, "Stopping\n");

	/* shutdown whatever old state might be left */
	if (apn->tun) {
		/* release tun device */
		LOGPAPN(LOGL_INFO, apn, "Closing TUN device %s\n",
			osmo_tundev_get_dev_name(apn->tun));
		osmo_tundev_close(apn->tun);
	}

	apn->started = false;
	return 0;
}