blob: 359c0f711ce87bc5fc1c61288b6c1147ca357b42 [file] [log] [blame]
Harald Welte5540c4c2010-05-19 14:38:50 +02001/* GPRS Networks Service (NS) messages on the Gb interface
2 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
4/* NS-over-FR-over-GRE implementation */
5
6/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26#include <errno.h>
27#include <string.h>
28#include <unistd.h>
29
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33
34#include <osmocore/select.h>
35#include <osmocore/msgb.h>
36#include <osmocore/talloc.h>
37
38#include <openbsc/socket.h>
39#include <openbsc/debug.h>
40#include <openbsc/gprs_ns.h>
41
42#define GRE_PTYPE_FR 0x6559
43
44struct gre_hdr {
45 uint16_t flags;
46 uint16_t ptype;
47} __attribute__ ((packed));
48
49static struct msgb *read_nsfrgre_msg(struct bsc_fd *bfd, int *error,
50 struct sockaddr_in *saddr)
51{
52 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx");
53 int ret = 0;
54 socklen_t saddr_len = sizeof(*saddr);
55 struct gre_hdr *greh;
56 uint8_t *frh;
57 uint32_t dlci;
58
59 if (!msg) {
60 *error = -ENOMEM;
61 return NULL;
62 }
63
64 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
65 (struct sockaddr *)saddr, &saddr_len);
66 if (ret < 0) {
67 LOGP(DNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n",
68 strerror(errno));
69 *error = ret;
Harald Welteffe191c2010-05-19 15:46:49 +020070 goto out_err;
Harald Welte5540c4c2010-05-19 14:38:50 +020071 } else if (ret == 0) {
Harald Welte5540c4c2010-05-19 14:38:50 +020072 *error = ret;
Harald Welteffe191c2010-05-19 15:46:49 +020073 goto out_err;
Harald Welte5540c4c2010-05-19 14:38:50 +020074 }
75
76 msgb_put(msg, ret);
77
78 if (msg->len < sizeof(*greh)) {
79 LOGP(DNS, LOGL_ERROR, "Short GRE packet: %u bytes\n", msg->len);
80 *error = -EIO;
81 goto out_err;
82 }
83
84 greh = (struct gre_hdr *) msg->data;
85 if (greh->flags) {
86 LOGP(DNS, LOGL_NOTICE, "Unknown GRE flags 0x%04x\n",
87 ntohs(greh->flags));
88 }
89 if (greh->ptype != htons(GRE_PTYPE_FR)) {
90 LOGP(DNS, LOGL_NOTICE, "Unknown GRE protocol 0x%04x != FR\n",
91 ntohs(greh->ptype));
92 *error = -EIO;
93 goto out_err;
94 }
95
96 if (msg->len < sizeof(*greh) + 2) {
97 LOGP(DNS, LOGL_ERROR, "Short FR header: %u bytes\n", msg->len);
98 *error = -EIO;
99 goto out_err;
100 }
101
102 frh = msg->data + sizeof(*greh);
103 if (frh[0] & 0x01) {
104 LOGP(DNS, LOGL_NOTICE, "Unsupported single-byte FR address\n");
105 *error = -EIO;
106 goto out_err;
107 }
108 dlci = (frh[0] & 0xfc << 2);
109 if ((frh[1] & 0x0f) != 0x01) {
110 LOGP(DNS, LOGL_NOTICE, "Unknown second FR octet 0x%02x\n",
111 frh[1]);
112 *error = -EIO;
113 goto out_err;
114 }
115 dlci |= frh[1] >> 4;
116 if (dlci > 0xffff) {
117 LOGP(DNS, LOGL_ERROR, "We don't support DLCI > 65535 (%u)\n",
118 dlci);
119 *error = -EINVAL;
120 goto out_err;
121 }
122
123 msg->l2h = msg->data + sizeof(*greh) + 2;
124
125 /* Store DLCI in NETWORK BYTEORDER in sockaddr port member */
126 saddr->sin_port = htons(dlci & 0xffff);
127
128 return msg;
129
130out_err:
131 msgb_free(msg);
132 return NULL;
133}
134
135int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
136 struct sockaddr_in *saddr, enum gprs_ns_ll ll);
137
138static int handle_nsfrgre_read(struct bsc_fd *bfd)
139{
140 int error;
141 struct sockaddr_in saddr;
142 struct gprs_ns_inst *nsi = bfd->data;
143 struct msgb *msg = read_nsfrgre_msg(bfd, &error, &saddr);
144
145 if (!msg)
146 return error;
147
148 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_FR_GRE);
149
150 msgb_free(msg);
151
152 return error;
153}
154
155static int handle_nsfrgre_write(struct bsc_fd *bfd)
156{
157 /* FIXME: actually send the data here instead of nsip_sendmsg() */
158 return -EIO;
159}
160
161int gprs_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
162{
163 int rc;
164 struct gprs_ns_inst *nsi = nsvc->nsi;
165 struct sockaddr_in daddr;
166 uint16_t dlci = ntohs(nsvc->frgre.bts_addr.sin_port);
167 uint8_t *frh;
168 struct gre_hdr *greh;
169
170 /* Build socket address for the packet destionation */
Harald Welte3c2a88d2010-05-19 15:37:34 +0200171 daddr.sin_family = AF_INET;
Harald Welte5540c4c2010-05-19 14:38:50 +0200172 daddr.sin_addr = nsvc->frgre.bts_addr.sin_addr;
173 daddr.sin_port = IPPROTO_GRE;
174
175 /* Prepend the FR header */
Harald Welte3c2a88d2010-05-19 15:37:34 +0200176 frh = msgb_push(msg, 2);
Harald Welte5540c4c2010-05-19 14:38:50 +0200177 frh[0] = (dlci >> 2) & 0xfc;
Harald Welte3c2a88d2010-05-19 15:37:34 +0200178 frh[1] = ((dlci & 0xf)<<4) | 0x01;
Harald Welte5540c4c2010-05-19 14:38:50 +0200179
180 /* Prepend the GRE header */
181 greh = (struct gre_hdr *) msgb_push(msg, sizeof(*greh));
182 greh->flags = 0;
Harald Welte3c2a88d2010-05-19 15:37:34 +0200183 greh->ptype = htons(GRE_PTYPE_FR);
Harald Welte5540c4c2010-05-19 14:38:50 +0200184
185 rc = sendto(nsi->frgre.fd.fd, msg->data, msg->len, 0,
186 (struct sockaddr *)&daddr, sizeof(daddr));
187
188 talloc_free(msg);
189
190 return rc;
191}
192
193static int nsfrgre_fd_cb(struct bsc_fd *bfd, unsigned int what)
194{
195 int rc = 0;
196
197 if (what & BSC_FD_READ)
198 rc = handle_nsfrgre_read(bfd);
199 if (what & BSC_FD_WRITE)
200 rc = handle_nsfrgre_write(bfd);
201
202 return rc;
203}
204
Harald Welteff3bde82010-05-19 15:09:09 +0200205int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi)
Harald Welte5540c4c2010-05-19 14:38:50 +0200206{
207 int rc;
208
209 /* Make sure we close any existing socket before changing it */
210 if (nsi->frgre.fd.fd)
211 close(nsi->frgre.fd.fd);
212
Harald Welteff3bde82010-05-19 15:09:09 +0200213 if (!nsi->frgre.enabled)
214 return 0;
215
216 rc = make_sock(&nsi->frgre.fd, IPPROTO_GRE, nsi->frgre.local_ip,
217 0, nsfrgre_fd_cb);
Harald Welte5540c4c2010-05-19 14:38:50 +0200218 if (rc < 0) {
219 LOGP(DNS, LOGL_ERROR, "Error creating GRE socket (%s)\n",
220 strerror(errno));
221 return rc;
222 }
223 nsi->frgre.fd.data = nsi;
224
225 return rc;
226}