aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-octphy/octpkt.c
blob: a6167f50c6c19e16c3047eac14cc02e7e006e317 (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
/* Utility routines for dealing with OCTPKT/OCTVC1 in msgb */

/* Copyright (c) 2015 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 <errno.h>
#include <unistd.h>

#include <arpa/inet.h>

#include <osmocom/core/select.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/socket.h>

#include <osmo-bts/gsm_data.h>

#include <octphy/octpkt/octpkt_hdr.h>
#include <octphy/octpkt/octpkt_hdr_swap.h>
#include <octphy/octvc1/octvocnet_pkt.h>
#include <octphy/octvc1/octvc1_msg.h>
#include <octphy/octvc1/octvc1_msg_swap.h>
#include <octphy/octvc1/gsm/octvc1_gsm_api.h>

#include "l1_if.h"
#include "octpkt.h"

/* push a common header (1 dword) to the start of a msgb */
void octpkt_push_common_hdr(struct msgb *msg, uint8_t format,
			    uint8_t trace, uint32_t ptype)
{
	uint32_t ch;
	uint32_t *chptr;
	uint32_t tot_len = msgb_length(msg) + sizeof(ch);

	ch = ((format & cOCTPKT_HDR_FORMAT_PROTO_TYPE_LEN_MASK_FORMAT_BIT_MASK)
		<< cOCTPKT_HDR_FORMAT_PROTO_TYPE_LEN_MASK_FORMAT_BIT_OFFSET) |
	     ((trace & cOCTPKT_HDR_FORMAT_PROTO_TYPE_LEN_MASK_TRACE_BIT_MASK)
	      	<< cOCTPKT_HDR_FORMAT_PROTO_TYPE_LEN_MASK_TRACE_BIT_OFFSET) |
	     ((ptype & cOCTPKT_HDR_FORMAT_PROTO_TYPE_LEN_MASK_CONTROL_PROTOCOL_TYPE_BIT_MASK)
	        << cOCTPKT_HDR_FORMAT_PROTO_TYPE_LEN_MASK_CONTROL_PROTOCOL_TYPE_BIT_OFFSET) |
	     (tot_len & cOCTPKT_HDR_FORMAT_PROTO_TYPE_LEN_MASK_LENGTH_BIT_MASK);

	chptr = (uint32_t *) msgb_push(msg, sizeof(ch));
	*chptr = htonl(ch);
}

/* push a control header (3 dwords) to the start of a msgb. This format
 * is used for command and response packets */
void octvocnet_push_ctl_hdr(struct msgb *msg, uint32_t dest_fifo_id,
			uint32_t src_fifo_id, uint32_t socket_id)
{
	tOCTVOCNET_PKT_CTL_HEADER *ch;

	ch = (tOCTVOCNET_PKT_CTL_HEADER *) msgb_push(msg, sizeof(*ch));

	ch->ulDestFifoId = htonl(dest_fifo_id);
	ch->ulSourceFifoId = htonl(src_fifo_id);
	ch->ulSocketId = htonl(socket_id);
}

/* push a data header (3 dwords) to the start of a msgb. This format is
 * used for event packets */
static void octvocnet_push_data_hdr(struct msgb *msg,
				 uint32_t log_obj_hdl,
				 uint32_t log_obj_pkt_port,
				 uint32_t dest_fifo_id)
{
	tOCTVOCNET_PKT_DATA_HEADER *dh;

	dh = (tOCTVOCNET_PKT_DATA_HEADER *) msgb_push(msg, sizeof(*dh));

	dh->hLogicalObj = htonl(log_obj_hdl);
	dh->ulLogicalObjPktPort = htonl(log_obj_pkt_port);
	dh->ulDestFifoId = htonl(dest_fifo_id);
}

/* common msg_header shared by all control messages. host byte order! */
void octvc1_fill_msg_hdr(tOCTVC1_MSG_HEADER *mh, uint32_t len,
			uint32_t sess_id, uint32_t trans_id,
			uint32_t user_info, uint32_t msg_type,
			uint32_t flags, uint32_t api_cmd)
{
	uint32_t type_r_cmdid;
	type_r_cmdid = ((msg_type & cOCTVC1_MSG_TYPE_BIT_MASK)
				<< cOCTVC1_MSG_TYPE_BIT_OFFSET) |
		       ((api_cmd & cOCTVC1_MSG_ID_BIT_MASK)
				<< cOCTVC1_MSG_ID_BIT_OFFSET);
	/* Resync?  Flags? */

	mh->ulLength = len;
	mh->ulTransactionId = trans_id;
	mh->ul_Type_R_CmdId = type_r_cmdid;
	mh->ulSessionId = sess_id;
	mh->ulReturnCode = 0;
	mh->ulUserInfo = user_info;
}

static void octvc1_push_msg_hdr(struct msgb *msg,
				uint32_t sess_id,
				uint32_t trans_id,
				uint32_t user_info,
				uint32_t msg_type,
				uint32_t flags,
				uint32_t api_cmd)
{
	tOCTVC1_MSG_HEADER *mh;
	uint32_t len = msgb_length(msg);

	mh = (tOCTVC1_MSG_HEADER *) msgb_push(msg, sizeof(*mh));

	octvc1_fill_msg_hdr(mh, len, sess_id, trans_id, user_info, msg_type, flags, api_cmd);
}

#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>

/*! \brief Initialize a packet socket
 *  \param[in] tye Socket type like SOCK_RAW or SOCK_DGRAM
 *  \param[in] proto The link-layer protocol in network byte order
 *  \param[in] bind_dev The name of the interface to bind to (if any)
 *  \param[in] flags flags like \ref OSMO_SOCK_F_BIND
 *
 * This function creates a new packet socket of \a type and \a proto
 * and optionally bnds to it, if stated in the \a flags parameter.
 */
int osmo_sock_packet_init(uint16_t type, uint16_t proto, const char *bind_dev,
			  unsigned int flags)
{
	int sfd, rc, on = 1;

	if (flags & OSMO_SOCK_F_CONNECT)
		return -EINVAL;

	sfd = socket(AF_PACKET, type, proto);
	if (sfd < 0)
		return -1;

	if (flags & OSMO_SOCK_F_NONBLOCK) {
		if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
			perror("cannot set this socket unblocking");
			close(sfd);
			return -EINVAL;
		}
	}

	if (bind_dev) {
		struct sockaddr_ll sa;
		struct ifreq ifr;

		/* resolve the string device name to an ifindex */
		memset(&ifr, 0, sizeof(ifr));
		strncpy(ifr.ifr_name, bind_dev, sizeof(ifr.ifr_name));
		rc = ioctl(sfd, SIOCGIFINDEX, &ifr);
		if (rc < 0)
			goto err;

		memset(&sa, 0, sizeof(sa));
		sa.sll_family = AF_PACKET;
		sa.sll_protocol = htons(proto);
		sa.sll_ifindex = ifr.ifr_ifindex;
		/* according to the packet(7) man page, bind() will only
		 * use sll_protocol nad sll_ifindex */
		rc = bind(sfd, (struct sockaddr *)&sa, sizeof(sa));
		if (rc < 0)
			goto err;
	}

	return sfd;
err:
	close(sfd);
	return -1;
}