aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-virtual/virtual_um.c
blob: 2126b0925f884ad49061008ac16ade80a45d93f1 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* 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 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 <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>

#include <osmocom/core/select.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/gsmtap.h>

#include "virtual_um.h"

#define VIRT_UM_MSGB_SIZE	256

static int mcast_join_group(int fd, const char *group, const char *netdev)
{
	int ifindex = 0;
	int rc, af;
	socklen_t af_len = sizeof(af);

	if (netdev) {
		ifindex = if_nametoindex(netdev);
		if (!ifindex)
			return -1;
	}

	rc = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &af_len);
	if (rc < 0)
		return rc;

	switch (af) {
	case AF_INET:
		{
			struct ip_mreqn mr;
			memset(&mr, 0, sizeof(mr));
			inet_pton(AF_INET, group, &mr.imr_multiaddr);
			if (ifindex)
				mr.imr_ifindex = ifindex;
			rc = setsockopt(fd, SOL_SOCKET, IP_ADD_MEMBERSHIP,
					&mr, sizeof(mr));
		}
		break;
	case AF_INET6:
		{
			struct ipv6_mreq mr;
			memset(&mr, 0, sizeof(mr));
			inet_pton(AF_INET6, group, &mr.ipv6mr_multiaddr);
			if (ifindex)
				mr.ipv6mr_interface = ifindex;
			rc = setsockopt(fd, SOL_SOCKET, IPV6_ADD_MEMBERSHIP,
					&mr, sizeof(mr));

		}
		break;
	default:
		rc = -1;
		break;
	}

	return rc;
}

static int mcast_connect(int fd, const char *group, uint16_t port)
{
	int rc, af;
	socklen_t af_len = sizeof(af);
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;

	rc = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &af_len);
	if (rc < 0)
		return rc;

	switch (af) {
	case AF_INET:
		memset(&sin, 0, sizeof(sin));
		sin.sin_family = AF_INET;
		sin.sin_port = htons(port);
		inet_pton(AF_INET, group, &sin.sin_addr);
		rc = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
		break;
	case AF_INET6:
		memset(&sin6, 0, sizeof(sin6));
		sin6.sin6_family = AF_INET6;
		sin6.sin6_port = htons(port);
		inet_pton(AF_INET6, group, &sin6.sin6_addr);
		rc = connect(fd, (struct sockaddr *) &sin6, sizeof(sin6));
		break;
	default:
		return -1;
	}

	return rc;
}

static int virt_um_fd_cb(struct osmo_fd *ofd, unsigned int what)
{
	struct virt_um_inst *vui = ofd->data;

	if (what & BSC_FD_READ) {
		struct msgb *msg = msgb_alloc(VIRT_UM_MSGB_SIZE, "Virtual UM Rx");
		int rc;

		rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
		if (rc > 0) {
			msgb_put(msg, rc);
			vui->recv_cb(vui, msg);
		} else {
			vui->recv_cb(vui, NULL);
			osmo_fd_unregister(ofd);
			close(ofd->fd);
			ofd->fd = -1;
			ofd->when = 0;
		}
	}

	return 0;
}

struct virt_um_inst *virt_um_init(void *ctx, const char *group, uint16_t port,
				  const char *netdev, void *priv,
				  void (*recv_cb)(struct virt_um_inst *vui, struct msgb *msg))
{
	struct virt_um_inst *vui;
	int fd, rc;

	if (!port)
		port = GSMTAP_UDP_PORT;
	if (!group)
		group = "239.0.47.29";

	/* crate a socked and bind it to the multicast group. Do NOT
	 * specify a fixed port locally, to make stack choose a random
	 * free UDP port. */
	fd = osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, group,
			    0, OSMO_SOCK_F_BIND);
	if (fd < 0)
		return NULL;

	/* join the multicast group */
	rc = mcast_join_group(fd, group, netdev);
	if (rc < 0) {
		close(fd);
		return NULL;
	}

	/* and finally also connect */
	rc = mcast_connect(fd, group, port);
	if (rc < 0) {
		close(fd);
		return NULL;
	}

	vui = talloc_zero(ctx, struct virt_um_inst);
	vui->priv = priv;
	vui->recv_cb = recv_cb;
	vui->ofd.data = vui;
	vui->ofd.fd = fd;
	vui->ofd.when = BSC_FD_READ;
	vui->ofd.cb = virt_um_fd_cb;

	osmo_fd_register(&vui->ofd);

	return vui;
}

void virt_um_destroy(struct virt_um_inst *vui)
{
	struct osmo_fd *ofd = &vui->ofd;

	osmo_fd_unregister(ofd);
	close(ofd->fd);
	ofd->fd = -1;
	ofd->when = 0;

	talloc_free(vui);
}

int virt_um_write_msg(struct virt_um_inst *vui, struct msgb *msg)
{
	int rc;

	rc = write(vui->ofd.fd, msgb_data(msg), msgb_length(msg));
	msgb_free(msg);

	return rc;
}