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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/* GPRS Networks Service (NS) messages on the Gb interface
* 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
/* NS-over-FR-over-GRE implementation */
/* (C) 2009-2010 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 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <osmocore/select.h>
#include <osmocore/msgb.h>
#include <osmocore/talloc.h>
#include <openbsc/socket.h>
#include <openbsc/debug.h>
#include <openbsc/gprs_ns.h>
#define GRE_PTYPE_FR 0x6559
struct gre_hdr {
uint16_t flags;
uint16_t ptype;
} __attribute__ ((packed));
static struct msgb *read_nsfrgre_msg(struct bsc_fd *bfd, int *error,
struct sockaddr_in *saddr)
{
struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx");
int ret = 0;
socklen_t saddr_len = sizeof(*saddr);
struct iphdr *iph;
struct gre_hdr *greh;
uint8_t *frh;
uint16_t dlci;
if (!msg) {
*error = -ENOMEM;
return NULL;
}
ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
(struct sockaddr *)saddr, &saddr_len);
if (ret < 0) {
LOGP(DNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n",
strerror(errno));
*error = ret;
goto out_err;
} else if (ret == 0) {
*error = ret;
goto out_err;
}
msgb_put(msg, ret);
if (msg->len < sizeof(*iph) + sizeof(*greh) + 2) {
LOGP(DNS, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len);
*error = -EIO;
goto out_err;
}
iph = (struct iphdr *) msg->data;
if (msg->len < (iph->ihl*4 + sizeof(*greh) + 2)) {
LOGP(DNS, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len);
*error = -EIO;
goto out_err;
}
greh = (struct gre_hdr *) (msg->data + iph->ihl*4);
if (greh->flags) {
LOGP(DNS, LOGL_NOTICE, "Unknown GRE flags 0x%04x\n",
ntohs(greh->flags));
}
if (greh->ptype != htons(GRE_PTYPE_FR)) {
LOGP(DNS, LOGL_NOTICE, "Unknown GRE protocol 0x%04x != FR\n",
ntohs(greh->ptype));
*error = -EIO;
goto out_err;
}
if (msg->len < sizeof(*greh) + 2) {
LOGP(DNS, LOGL_ERROR, "Short FR header: %u bytes\n", msg->len);
*error = -EIO;
goto out_err;
}
frh = (uint8_t *)greh + sizeof(*greh);
if (frh[0] & 0x01) {
LOGP(DNS, LOGL_NOTICE, "Unsupported single-byte FR address\n");
*error = -EIO;
goto out_err;
}
dlci = ((frh[0] & 0xfc) << 2);
if ((frh[1] & 0x0f) != 0x01) {
LOGP(DNS, LOGL_NOTICE, "Unknown second FR octet 0x%02x\n",
frh[1]);
*error = -EIO;
goto out_err;
}
dlci |= (frh[1] >> 4);
msg->l2h = frh+2;
/* Store DLCI in NETWORK BYTEORDER in sockaddr port member */
saddr->sin_port = htons(dlci);
return msg;
out_err:
msgb_free(msg);
return NULL;
}
int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
struct sockaddr_in *saddr, enum gprs_ns_ll ll);
static int handle_nsfrgre_read(struct bsc_fd *bfd)
{
int error;
struct sockaddr_in saddr;
struct gprs_ns_inst *nsi = bfd->data;
struct msgb *msg = read_nsfrgre_msg(bfd, &error, &saddr);
if (!msg)
return error;
error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_FR_GRE);
msgb_free(msg);
return error;
}
static int handle_nsfrgre_write(struct bsc_fd *bfd)
{
/* FIXME: actually send the data here instead of nsip_sendmsg() */
return -EIO;
}
int gprs_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
{
int rc;
struct gprs_ns_inst *nsi = nsvc->nsi;
struct sockaddr_in daddr;
uint16_t dlci = ntohs(nsvc->frgre.bts_addr.sin_port);
uint8_t *frh;
struct gre_hdr *greh;
/* Build socket address for the packet destionation */
daddr.sin_family = AF_INET;
daddr.sin_addr = nsvc->frgre.bts_addr.sin_addr;
daddr.sin_port = IPPROTO_GRE;
/* Prepend the FR header */
frh = msgb_push(msg, 2);
frh[0] = (dlci >> 2) & 0xfc;
frh[1] = ((dlci & 0xf)<<4) | 0x01;
/* Prepend the GRE header */
greh = (struct gre_hdr *) msgb_push(msg, sizeof(*greh));
greh->flags = 0;
greh->ptype = htons(GRE_PTYPE_FR);
rc = sendto(nsi->frgre.fd.fd, msg->data, msg->len, 0,
(struct sockaddr *)&daddr, sizeof(daddr));
talloc_free(msg);
return rc;
}
static int nsfrgre_fd_cb(struct bsc_fd *bfd, unsigned int what)
{
int rc = 0;
if (what & BSC_FD_READ)
rc = handle_nsfrgre_read(bfd);
if (what & BSC_FD_WRITE)
rc = handle_nsfrgre_write(bfd);
return rc;
}
int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi)
{
int rc;
/* Make sure we close any existing socket before changing it */
if (nsi->frgre.fd.fd)
close(nsi->frgre.fd.fd);
if (!nsi->frgre.enabled)
return 0;
rc = make_sock(&nsi->frgre.fd, IPPROTO_GRE, nsi->frgre.local_ip,
0, nsfrgre_fd_cb);
if (rc < 0) {
LOGP(DNS, LOGL_ERROR, "Error creating GRE socket (%s)\n",
strerror(errno));
return rc;
}
nsi->frgre.fd.data = nsi;
return rc;
}
|