blob: de5149c5ab66192cc24391b9e42f1e35a08b71ac [file] [log] [blame]
Harald Welte5fd8a542009-02-13 02:43:36 +00001/* OpenBSC Abis input driver for ip.access */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdio.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <string.h>
28#include <time.h>
29#include <sys/fcntl.h>
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/ioctl.h>
33#include <arpa/inet.h>
34
Harald Weltedfe6c7d2010-02-20 16:24:02 +010035#include <osmocore/select.h>
36#include <osmocore/tlv.h>
37#include <osmocore/msgb.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000038#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
40#include <openbsc/abis_nm.h>
41#include <openbsc/abis_rsl.h>
42#include <openbsc/subchan_demux.h>
43#include <openbsc/e1_input.h>
Harald Welte4f361fc2009-02-15 15:32:53 +000044#include <openbsc/ipaccess.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010045#include <osmocore/talloc.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000046
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +010047#define PRIV_OML 1
48#define PRIV_RSL 2
49
Harald Welte5fd8a542009-02-13 02:43:36 +000050/* data structure for one E1 interface with A-bis */
51struct ia_e1_handle {
52 struct bsc_fd listen_fd;
Harald Welte5c1e4582009-02-15 11:57:29 +000053 struct bsc_fd rsl_listen_fd;
Harald Welteedb37782009-05-01 14:59:07 +000054 struct gsm_network *gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +000055};
56
Harald Welteedb37782009-05-01 14:59:07 +000057static struct ia_e1_handle *e1h;
58
59
Harald Welte5fd8a542009-02-13 02:43:36 +000060#define TS1_ALLOC_SIZE 300
61
Harald Welte4f361fc2009-02-15 15:32:53 +000062static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG };
63static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK };
Harald Welteedb37782009-05-01 14:59:07 +000064static const u_int8_t id_req[] = { 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
65 0x01, IPAC_IDTAG_UNIT,
66 0x01, IPAC_IDTAG_MACADDR,
67 0x01, IPAC_IDTAG_LOCATION1,
68 0x01, IPAC_IDTAG_LOCATION2,
69 0x01, IPAC_IDTAG_EQUIPVERS,
70 0x01, IPAC_IDTAG_SWVERSION,
71 0x01, IPAC_IDTAG_UNITNAME,
72 0x01, IPAC_IDTAG_SERNR,
73 };
Harald Welte5fd8a542009-02-13 02:43:36 +000074
Harald Welteedb37782009-05-01 14:59:07 +000075static const char *idtag_names[] = {
76 [IPAC_IDTAG_SERNR] = "Serial_Number",
77 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
78 [IPAC_IDTAG_LOCATION1] = "Location_1",
79 [IPAC_IDTAG_LOCATION2] = "Location_2",
80 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
81 [IPAC_IDTAG_SWVERSION] = "Software_Version",
82 [IPAC_IDTAG_IPADDR] = "IP_Address",
83 [IPAC_IDTAG_MACADDR] = "MAC_Address",
84 [IPAC_IDTAG_UNIT] = "Unit_ID",
85};
86
87static const char *ipac_idtag_name(int tag)
Harald Welte5fd8a542009-02-13 02:43:36 +000088{
Harald Welteedb37782009-05-01 14:59:07 +000089 if (tag >= ARRAY_SIZE(idtag_names))
90 return "unknown";
91
92 return idtag_names[tag];
93}
94
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +010095int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
Harald Welteedb37782009-05-01 14:59:07 +000096{
97 u_int8_t t_len;
98 u_int8_t t_tag;
99 u_int8_t *cur = buf;
100
101 while (cur < buf + len) {
102 t_len = *cur++;
103 t_tag = *cur++;
104
105 DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur);
106
107 dec->lv[t_tag].len = t_len;
108 dec->lv[t_tag].val = cur;
109
110 cur += t_len;
111 }
112 return 0;
113}
114
115struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
116 u_int16_t site_id, u_int16_t bts_id)
117{
Harald Weltee441d9c2009-06-21 16:17:15 +0200118 struct gsm_bts *bts;
Harald Welteedb37782009-05-01 14:59:07 +0000119
Harald Weltee441d9c2009-06-21 16:17:15 +0200120 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000121
122 if (!is_ipaccess_bts(bts))
123 continue;
124
125 if (bts->ip_access.site_id == site_id &&
126 bts->ip_access.bts_id == bts_id)
127 return bts;
128 }
129
130 return NULL;
131}
132
133static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
134 u_int16_t *trx_id)
135{
136 unsigned long ul;
137 char *endptr;
138 const char *nptr;
139
140 nptr = str;
141 ul = strtoul(nptr, &endptr, 10);
142 if (endptr <= nptr)
143 return -EINVAL;
144 if (site_id)
145 *site_id = ul & 0xffff;
146
147 if (*endptr++ != '/')
148 return -EINVAL;
149
150 nptr = endptr;
151 ul = strtoul(nptr, &endptr, 10);
152 if (endptr <= nptr)
153 return -EINVAL;
154 if (bts_id)
155 *bts_id = ul & 0xffff;
156
157 if (*endptr++ != '/')
158 return -EINVAL;
159
160 nptr = endptr;
161 ul = strtoul(nptr, &endptr, 10);
162 if (endptr <= nptr)
163 return -EINVAL;
164 if (trx_id)
165 *trx_id = ul & 0xffff;
166
167 return 0;
168}
169
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100170/* send the id ack */
171int ipaccess_send_id_ack(int fd)
172{
173 return write(fd, id_ack, sizeof(id_ack));
174}
175
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100176int ipaccess_send_id_req(int fd)
177{
178 return write(fd, id_req, sizeof(id_req));
179}
180
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200181/* base handling of the ip.access protocol */
182int ipaccess_rcvmsg_base(struct msgb *msg,
183 struct bsc_fd *bfd)
Harald Welteedb37782009-05-01 14:59:07 +0000184{
Harald Welte5fd8a542009-02-13 02:43:36 +0000185 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +0000186 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000187
Harald Welte5fd8a542009-02-13 02:43:36 +0000188 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000189 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000190 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000191 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000192 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000193 DEBUGP(DMI, "PONG!\n");
194 break;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200195 case IPAC_MSGT_ID_ACK:
196 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100197 ret = ipaccess_send_id_ack(bfd->fd);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200198 break;
199 }
200 return 0;
201}
202
203static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
204 struct bsc_fd *bfd)
205{
206 struct tlv_parsed tlvp;
207 u_int8_t msg_type = *(msg->l2h);
208 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
209 struct gsm_bts *bts;
210
211 /* handle base messages */
212 ipaccess_rcvmsg_base(msg, bfd);
213
214 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000215 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000216 DEBUGP(DMI, "ID_RESP ");
217 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +0100218 ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
Harald Welteedb37782009-05-01 14:59:07 +0000219 msgb_l2len(msg)-2);
220 DEBUGP(DMI, "\n");
221
222 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
223 break;
224
225 /* lookup BTS, create sign_link, ... */
226 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
227 &site_id, &bts_id, &trx_id);
228 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
229 if (!bts) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100230 LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for "
Harald Welteedb37782009-05-01 14:59:07 +0000231 " %u/%u/%u, disconnecting\n", site_id, bts_id,
232 trx_id);
233 return -EIO;
234 }
235 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100236 if (bfd->priv_nr == PRIV_OML) {
237 bts->oml_link = e1inp_sign_link_create(&line->ts[PRIV_OML - 1],
Harald Welteedb37782009-05-01 14:59:07 +0000238 E1INP_SIGN_OML, bts->c0,
Harald Welte8175e952009-10-20 00:22:00 +0200239 bts->oml_tei, 0);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100240 } else if (bfd->priv_nr == PRIV_RSL) {
Harald Welteedb37782009-05-01 14:59:07 +0000241 struct e1inp_ts *e1i_ts;
242 struct bsc_fd *newbfd;
Harald Welte8175e952009-10-20 00:22:00 +0200243 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id);
Harald Welteedb37782009-05-01 14:59:07 +0000244
Harald Welteedb37782009-05-01 14:59:07 +0000245 bfd->data = line = bts->oml_link->ts->line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100246 e1i_ts = &line->ts[PRIV_RSL + trx_id - 1];
Harald Welteedb37782009-05-01 14:59:07 +0000247 newbfd = &e1i_ts->driver.ipaccess.fd;
Harald Welte8175e952009-10-20 00:22:00 +0200248 e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000249
Harald Welte8175e952009-10-20 00:22:00 +0200250 trx->rsl_link = e1inp_sign_link_create(e1i_ts,
251 E1INP_SIGN_RSL, trx,
252 trx->rsl_tei, 0);
Holger Hans Peter Freyther354ef812010-03-24 04:02:55 +0100253
254 if (newbfd->fd >= 0) {
255 LOGP(DINP, LOGL_ERROR, "BTS is still registered. Closing old connection.\n");
256 bsc_unregister_fd(newbfd);
257 close(newbfd->fd);
258 }
259
Harald Welteedb37782009-05-01 14:59:07 +0000260 /* get rid of our old temporary bfd */
261 memcpy(newbfd, bfd, sizeof(*newbfd));
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100262 newbfd->priv_nr = PRIV_RSL + trx_id;
Harald Welteedb37782009-05-01 14:59:07 +0000263 bsc_unregister_fd(bfd);
264 bsc_register_fd(newbfd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200265 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000266 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000267 break;
Harald Welte5fd8a542009-02-13 02:43:36 +0000268 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000269 return 0;
270}
271
Harald Welte88a412a2009-12-16 17:32:37 +0100272#define OML_UP 0x0001
273#define RSL_UP 0x0002
Harald Welte7782c142009-02-15 03:39:51 +0000274
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200275/*
276 * read one ipa message from the socket
277 * return NULL in case of error
278 */
279struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte5fd8a542009-02-13 02:43:36 +0000280{
Harald Welte966636f2009-06-26 19:39:35 +0200281 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000282 struct ipaccess_head *hh;
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100283 int len, ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000284
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200285 if (!msg) {
286 *error = -ENOMEM;
287 return NULL;
288 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000289
290 /* first read our 3-byte header */
291 hh = (struct ipaccess_head *) msg->data;
292 ret = recv(bfd->fd, msg->data, 3, 0);
293 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100294 if (errno != EAGAIN)
295 LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200296 msgb_free(msg);
297 *error = ret;
298 return NULL;
299 } else if (ret == 0) {
300 msgb_free(msg);
301 *error = ret;
302 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000303 }
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200304
Harald Welte5fd8a542009-02-13 02:43:36 +0000305 msgb_put(msg, ret);
306
307 /* then read te length as specified in header */
308 msg->l2h = msg->data + sizeof(*hh);
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100309 len = ntohs(hh->len);
310 ret = recv(bfd->fd, msg->l2h, len, 0);
311 if (ret < len) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100312 LOGP(DINP, LOGL_ERROR, "short read!\n");
Harald Welte5c1e4582009-02-15 11:57:29 +0000313 msgb_free(msg);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200314 *error = -EIO;
315 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000316 }
317 msgb_put(msg, ret);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200318
319 return msg;
320}
321
322static int handle_ts1_read(struct bsc_fd *bfd)
323{
324 struct e1inp_line *line = bfd->data;
325 unsigned int ts_nr = bfd->priv_nr;
326 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
327 struct e1inp_sign_link *link;
328 struct msgb *msg;
329 struct ipaccess_head *hh;
Holger Hans Peter Freyther67b59612009-10-27 10:08:38 +0100330 int ret = 0, error;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200331
332 msg = ipaccess_read_msg(bfd, &error);
333 if (!msg) {
334 if (error == 0) {
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100335 link = e1inp_lookup_sign_link(e1i_ts, IPAC_PROTO_OML, 0);
336 if (link) {
337 link->trx->bts->ip_access.flags = 0;
338 LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n",
339 link->trx->bts->nr);
340 } else
341 LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n");
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200342 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
343 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
344 bsc_unregister_fd(bfd);
345 close(bfd->fd);
346 bfd->fd = -1;
347 }
348 return error;
349 }
350
Sylvain Munaut86538c72009-09-30 22:35:04 +0200351 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000352
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200353 hh = (struct ipaccess_head *) msg->data;
Harald Welteedb37782009-05-01 14:59:07 +0000354 if (hh->proto == IPAC_PROTO_IPACCESS) {
355 ret = ipaccess_rcvmsg(line, msg, bfd);
356 if (ret < 0) {
357 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
358 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
359 bsc_unregister_fd(bfd);
360 close(bfd->fd);
361 bfd->fd = -1;
362 }
363 msgb_free(msg);
364 return ret;
365 }
366 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
367 * might have free'd it !!! */
368
Harald Welte8175e952009-10-20 00:22:00 +0200369 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
Harald Welte5fd8a542009-02-13 02:43:36 +0000370 if (!link) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100371 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
372 "hh->proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000373 msgb_free(msg);
374 return -EIO;
375 }
376 msg->trx = link->trx;
377
Harald Welte8175e952009-10-20 00:22:00 +0200378 switch (link->type) {
379 case E1INP_SIGN_RSL:
Harald Welte1394fea2009-12-21 23:01:33 +0100380 if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) {
Harald Weltee73501a2009-10-21 21:16:00 +0200381 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte1394fea2009-12-21 23:01:33 +0100382 msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr);
Harald Weltee73501a2009-10-21 21:16:00 +0200383 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000384 ret = abis_rsl_rcvmsg(msg);
385 break;
Harald Welte8175e952009-10-20 00:22:00 +0200386 case E1INP_SIGN_OML:
Harald Welte88a412a2009-12-16 17:32:37 +0100387 if (!(msg->trx->bts->ip_access.flags & OML_UP)) {
Harald Welte8175e952009-10-20 00:22:00 +0200388 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte88a412a2009-12-16 17:32:37 +0100389 msg->trx->bts->ip_access.flags |= OML_UP;
Harald Welte7782c142009-02-15 03:39:51 +0000390 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000391 ret = abis_nm_rcvmsg(msg);
392 break;
393 default:
Harald Welteafdca0f2009-12-23 23:01:04 +0100394 LOGP(DINP, LOGL_NOTICE, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000395 msgb_free(msg);
396 break;
397 }
398 return ret;
399}
400
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200401void ipaccess_prepend_header(struct msgb *msg, int proto)
402{
403 struct ipaccess_head *hh;
404
405 /* prepend the ip.access header */
406 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100407 hh->len = htons(msg->len - sizeof(*hh));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200408 hh->proto = proto;
409}
410
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200411static int ts_want_write(struct e1inp_ts *e1i_ts)
412{
413 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
414
415 return 0;
416}
417
418static void timeout_ts1_write(void *data)
419{
420 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
421
422 /* trigger write of ts1, due to tx delay timer */
423 ts_want_write(e1i_ts);
424}
425
Harald Welte5fd8a542009-02-13 02:43:36 +0000426static int handle_ts1_write(struct bsc_fd *bfd)
427{
428 struct e1inp_line *line = bfd->data;
429 unsigned int ts_nr = bfd->priv_nr;
430 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
431 struct e1inp_sign_link *sign_link;
432 struct msgb *msg;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200433 u_int8_t proto;
Harald Welte5fd8a542009-02-13 02:43:36 +0000434 int ret;
435
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200436 bfd->when &= ~BSC_FD_WRITE;
437
Harald Welte5fd8a542009-02-13 02:43:36 +0000438 /* get the next msg for this timeslot */
439 msg = e1inp_tx_ts(e1i_ts, &sign_link);
440 if (!msg) {
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200441 /* no message after tx delay timer */
Harald Welte5fd8a542009-02-13 02:43:36 +0000442 return 0;
443 }
444
Harald Welte5fd8a542009-02-13 02:43:36 +0000445 switch (sign_link->type) {
446 case E1INP_SIGN_OML:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200447 proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000448 break;
449 case E1INP_SIGN_RSL:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200450 proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000451 break;
452 default:
453 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200454 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Harald Welte5fd8a542009-02-13 02:43:36 +0000455 return -EINVAL;
456 }
457
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200458 msg->l2h = msg->data;
Harald Welte8175e952009-10-20 00:22:00 +0200459 ipaccess_prepend_header(msg, sign_link->tei);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200460
461 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000462
463 ret = send(bfd->fd, msg->data, msg->len, 0);
464 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200465
466 /* set tx delay timer for next event */
467 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
468 e1i_ts->sign.tx_timer.data = e1i_ts;
469 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000470
471 return ret;
472}
473
Harald Welte5fd8a542009-02-13 02:43:36 +0000474/* callback from select.c in case one of the fd's can be read/written */
475static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
476{
477 struct e1inp_line *line = bfd->data;
478 unsigned int ts_nr = bfd->priv_nr;
479 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000480 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000481 int rc = 0;
482
Harald Welteedb37782009-05-01 14:59:07 +0000483 /* In case of early RSL we might not yet have a line */
484
485 if (line)
486 e1i_ts = &line->ts[idx];
487
488 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000489 if (what & BSC_FD_READ)
490 rc = handle_ts1_read(bfd);
491 if (what & BSC_FD_WRITE)
492 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000493 } else
Harald Welteafdca0f2009-12-23 23:01:04 +0100494 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000495
496 return rc;
497}
498
499
Harald Welte5fd8a542009-02-13 02:43:36 +0000500struct e1inp_driver ipaccess_driver = {
501 .name = "ip.access",
502 .want_write = ts_want_write,
503};
504
Harald Welteedb37782009-05-01 14:59:07 +0000505/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000506static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
507{
Harald Welte5fd8a542009-02-13 02:43:36 +0000508 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000509 int idx = 0;
510 struct e1inp_line *line;
511 struct e1inp_ts *e1i_ts;
512 struct bsc_fd *bfd;
513 struct sockaddr_in sa;
514 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000515
Harald Welteedb37782009-05-01 14:59:07 +0000516 if (!(what & BSC_FD_READ))
517 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000518
Harald Welteedb37782009-05-01 14:59:07 +0000519 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
520 if (ret < 0) {
521 perror("accept");
522 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000523 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100524 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
525 inet_ntoa(sa.sin_addr));
Harald Welteedb37782009-05-01 14:59:07 +0000526
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100527 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000528 if (!line) {
529 close(ret);
530 return -ENOMEM;
531 }
Harald Welteedb37782009-05-01 14:59:07 +0000532 line->driver = &ipaccess_driver;
533 //line->driver_data = e1h;
534 /* create virrtual E1 timeslots for signalling */
535 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000536
537 e1i_ts = &line->ts[idx];
538
539 bfd = &e1i_ts->driver.ipaccess.fd;
540 bfd->fd = ret;
541 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100542 bfd->priv_nr = PRIV_OML;
Harald Welteedb37782009-05-01 14:59:07 +0000543 bfd->cb = ipaccess_fd_cb;
544 bfd->when = BSC_FD_READ;
545 ret = bsc_register_fd(bfd);
546 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100547 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000548 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200549 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000550 return ret;
551 }
552
553 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100554 ret = ipaccess_send_id_req(bfd->fd);
Harald Welteedb37782009-05-01 14:59:07 +0000555
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200556 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200557 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000558}
559
Harald Welte5c1e4582009-02-15 11:57:29 +0000560static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
561{
Harald Welteedb37782009-05-01 14:59:07 +0000562 struct sockaddr_in sa;
563 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200564 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000565 int ret;
566
Harald Welteedb37782009-05-01 14:59:07 +0000567 if (!(what & BSC_FD_READ))
568 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000569
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100570 bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200571 if (!bfd)
572 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200573
Harald Welteedb37782009-05-01 14:59:07 +0000574 /* Some BTS has connected to us, but we don't know yet which line
575 * (as created by the OML link) to associate it with. Thus, we
Holger Hans Peter Freytherc7df7c62009-11-15 13:50:39 +0100576 * allocate a temporary bfd until we have received ID from BTS */
Harald Welteedb37782009-05-01 14:59:07 +0000577
578 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
579 if (bfd->fd < 0) {
580 perror("accept");
581 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000582 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100583 LOGP(DINP, LOGL_NOTICE, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100584 bfd->priv_nr = PRIV_RSL;
Harald Welteedb37782009-05-01 14:59:07 +0000585 bfd->cb = ipaccess_fd_cb;
586 bfd->when = BSC_FD_READ;
587 ret = bsc_register_fd(bfd);
588 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100589 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000590 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200591 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000592 return ret;
593 }
594 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
595 ret = write(bfd->fd, id_req, sizeof(id_req));
596
Harald Welte5c1e4582009-02-15 11:57:29 +0000597 return 0;
598}
599
600static int make_sock(struct bsc_fd *bfd, u_int16_t port,
Harald Welte5c1e4582009-02-15 11:57:29 +0000601 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000602{
603 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000604 int ret, on = 1;
605
606 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
607 bfd->cb = cb;
608 bfd->when = BSC_FD_READ;
Harald Welteedb37782009-05-01 14:59:07 +0000609 //bfd->data = line;
Harald Welte5c1e4582009-02-15 11:57:29 +0000610
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100611 if (bfd->fd < 0) {
612 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
613 return -EIO;
614 }
615
Harald Welte5c1e4582009-02-15 11:57:29 +0000616 memset(&addr, 0, sizeof(addr));
617 addr.sin_family = AF_INET;
618 addr.sin_port = htons(port);
619 addr.sin_addr.s_addr = INADDR_ANY;
620
621 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
622
623 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
624 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100625 LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n",
Harald Welte5c1e4582009-02-15 11:57:29 +0000626 strerror(errno));
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100627 close(bfd->fd);
Harald Welte5c1e4582009-02-15 11:57:29 +0000628 return -EIO;
629 }
630
631 ret = listen(bfd->fd, 1);
632 if (ret < 0) {
633 perror("listen");
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100634 close(bfd->fd);
Harald Welte5c1e4582009-02-15 11:57:29 +0000635 return ret;
636 }
637
638 ret = bsc_register_fd(bfd);
639 if (ret < 0) {
640 perror("register_listen_fd");
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100641 close(bfd->fd);
Harald Welte5c1e4582009-02-15 11:57:29 +0000642 return ret;
643 }
644 return 0;
645}
646
Harald Welte25de9912009-04-30 15:53:07 +0000647/* Actively connect to a BTS. Currently used by ipaccess-config.c */
648int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
649{
650 struct e1inp_ts *e1i_ts = &line->ts[0];
651 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
652 int ret, on = 1;
653
654 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
655 bfd->cb = ipaccess_fd_cb;
656 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
657 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100658 bfd->priv_nr = PRIV_OML;
Harald Welte25de9912009-04-30 15:53:07 +0000659
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100660 if (bfd->fd < 0) {
661 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
662 return -EIO;
663 }
664
Harald Welte25de9912009-04-30 15:53:07 +0000665 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
666
667 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
668 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100669 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
Harald Welte25de9912009-04-30 15:53:07 +0000670 close(bfd->fd);
671 return ret;
672 }
673
674 ret = bsc_register_fd(bfd);
675 if (ret < 0) {
676 close(bfd->fd);
677 return ret;
678 }
679
680 line->driver = &ipaccess_driver;
681
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200682 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200683 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000684}
685
Harald Welteedb37782009-05-01 14:59:07 +0000686int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000687{
Harald Welte5c1e4582009-02-15 11:57:29 +0000688 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000689
690 /* register the driver with the core */
691 /* FIXME: do this in the plugin initializer function */
692 ret = e1inp_driver_register(&ipaccess_driver);
693 if (ret)
694 return ret;
695
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100696 e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200697 if (!e1h)
698 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200699
Harald Welteedb37782009-05-01 14:59:07 +0000700 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000701
Harald Welte5c1e4582009-02-15 11:57:29 +0000702 /* Listen for OML connections */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100703 ret = make_sock(&e1h->listen_fd, IPA_TCP_PORT_OML, listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000704 if (ret < 0)
705 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000706
Harald Welte5c1e4582009-02-15 11:57:29 +0000707 /* Listen for RSL connections */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100708 ret = make_sock(&e1h->rsl_listen_fd, IPA_TCP_PORT_RSL, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000709
Harald Welteedb37782009-05-01 14:59:07 +0000710 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000711}