aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-virtual/virtual_um.c
blob: 711e75d3f5818f7dab8e3befd5e34dc0e441d38b (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
108
109
110
111
112
113
114
115
/* Routines for a Virtual Um interface over GSMTAP/UDP */

/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <osmocom/core/select.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/gsmtap.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/talloc.h>
#include "osmo_mcast_sock.h"
#include "virtual_um.h"

#include <unistd.h>
#include <errno.h>

/**
 * Virtual UM interface file descriptor read callback.
 */
static void virt_um_read_cb(int rc, struct msgb *msg, void *data)
{
	struct virt_um_inst *vui = data;
	msg->l1h = msg->data;

	/* call the l1 callback function for a received msg */
	vui->recv_cb(vui, msg);
}

struct virt_um_inst *virt_um_init(void *ctx, char *tx_mcast_group, uint16_t tx_mcast_port,
				  char *rx_mcast_group, uint16_t rx_mcast_port, int ttl, const char *dev_name,
				  void (*recv_cb)(struct virt_um_inst *vui, struct msgb *msg))
{
	struct virt_um_inst *vui = talloc_zero(ctx, struct virt_um_inst);
	int rc;

	vui->mcast_sock = mcast_bidir_sock_setup(ctx, tx_mcast_group, tx_mcast_port,
						 rx_mcast_group, rx_mcast_port, 1, virt_um_read_cb, vui);
	if (!vui->mcast_sock) {
		perror("Unable to create VirtualUm multicast socket");
		talloc_free(vui);
		return NULL;
	}
	vui->recv_cb = recv_cb;

	/* -1 means default, i.e. no TTL explicitly configured in VTY */
	if (ttl >= 0) {
		int txfd = osmo_iofd_get_fd(vui->mcast_sock->tx_iofd);
		rc = osmo_sock_mcast_ttl_set(txfd, ttl);
		if (rc < 0) {
			perror("Cannot set TTL of Virtual Um transmit socket");
			goto out_close;
		}
	}

	if (dev_name) {
		int txfd = osmo_iofd_get_fd(vui->mcast_sock->tx_iofd);
		rc = osmo_sock_mcast_iface_set(txfd, dev_name);
		if (rc < 0) {
			perror("Cannot bind multicast tx to given device");
			goto out_close;
		}
		int rxfd = osmo_iofd_get_fd(vui->mcast_sock->rx_iofd);
		rc = osmo_sock_mcast_iface_set(rxfd, dev_name);
		if (rc < 0) {
			perror("Cannot bind multicast rx to given device");
			goto out_close;
		}
	}

	return vui;

out_close:
	mcast_bidir_sock_close(vui->mcast_sock);
	talloc_free(vui);
	return NULL;
}

void virt_um_destroy(struct virt_um_inst *vui)
{
	mcast_bidir_sock_close(vui->mcast_sock);
	talloc_free(vui);
}

/**
 * Write msg to to multicast socket and free msg afterwards
 */
int virt_um_write_msg(struct virt_um_inst *vui, struct msgb *msg)
{
	int rc;

	rc = mcast_bidir_sock_tx_msg(vui->mcast_sock, msg);
	if (rc < 0) {
		msgb_free(msg);
		rc = -errno;
	}

	return rc;
}