blob: 71a3bde0a491b8f900dc1be6e27839fb92cf9ccb [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
47/* data structure for one E1 interface with A-bis */
48struct ia_e1_handle {
49 struct bsc_fd listen_fd;
Harald Welte5c1e4582009-02-15 11:57:29 +000050 struct bsc_fd rsl_listen_fd;
Harald Welteedb37782009-05-01 14:59:07 +000051 struct gsm_network *gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +000052};
53
Harald Welteedb37782009-05-01 14:59:07 +000054static struct ia_e1_handle *e1h;
55
56
Harald Welte5fd8a542009-02-13 02:43:36 +000057#define TS1_ALLOC_SIZE 300
58
Harald Welte4f361fc2009-02-15 15:32:53 +000059static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG };
60static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK };
Harald Welteedb37782009-05-01 14:59:07 +000061static const u_int8_t id_req[] = { 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
62 0x01, IPAC_IDTAG_UNIT,
63 0x01, IPAC_IDTAG_MACADDR,
64 0x01, IPAC_IDTAG_LOCATION1,
65 0x01, IPAC_IDTAG_LOCATION2,
66 0x01, IPAC_IDTAG_EQUIPVERS,
67 0x01, IPAC_IDTAG_SWVERSION,
68 0x01, IPAC_IDTAG_UNITNAME,
69 0x01, IPAC_IDTAG_SERNR,
70 };
Harald Welte5fd8a542009-02-13 02:43:36 +000071
Harald Welteedb37782009-05-01 14:59:07 +000072static const char *idtag_names[] = {
73 [IPAC_IDTAG_SERNR] = "Serial_Number",
74 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
75 [IPAC_IDTAG_LOCATION1] = "Location_1",
76 [IPAC_IDTAG_LOCATION2] = "Location_2",
77 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
78 [IPAC_IDTAG_SWVERSION] = "Software_Version",
79 [IPAC_IDTAG_IPADDR] = "IP_Address",
80 [IPAC_IDTAG_MACADDR] = "MAC_Address",
81 [IPAC_IDTAG_UNIT] = "Unit_ID",
82};
83
84static const char *ipac_idtag_name(int tag)
Harald Welte5fd8a542009-02-13 02:43:36 +000085{
Harald Welteedb37782009-05-01 14:59:07 +000086 if (tag >= ARRAY_SIZE(idtag_names))
87 return "unknown";
88
89 return idtag_names[tag];
90}
91
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +010092int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
Harald Welteedb37782009-05-01 14:59:07 +000093{
94 u_int8_t t_len;
95 u_int8_t t_tag;
96 u_int8_t *cur = buf;
97
98 while (cur < buf + len) {
99 t_len = *cur++;
100 t_tag = *cur++;
101
102 DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur);
103
104 dec->lv[t_tag].len = t_len;
105 dec->lv[t_tag].val = cur;
106
107 cur += t_len;
108 }
109 return 0;
110}
111
112struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
113 u_int16_t site_id, u_int16_t bts_id)
114{
Harald Weltee441d9c2009-06-21 16:17:15 +0200115 struct gsm_bts *bts;
Harald Welteedb37782009-05-01 14:59:07 +0000116
Harald Weltee441d9c2009-06-21 16:17:15 +0200117 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000118
119 if (!is_ipaccess_bts(bts))
120 continue;
121
122 if (bts->ip_access.site_id == site_id &&
123 bts->ip_access.bts_id == bts_id)
124 return bts;
125 }
126
127 return NULL;
128}
129
130static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
131 u_int16_t *trx_id)
132{
133 unsigned long ul;
134 char *endptr;
135 const char *nptr;
136
137 nptr = str;
138 ul = strtoul(nptr, &endptr, 10);
139 if (endptr <= nptr)
140 return -EINVAL;
141 if (site_id)
142 *site_id = ul & 0xffff;
143
144 if (*endptr++ != '/')
145 return -EINVAL;
146
147 nptr = endptr;
148 ul = strtoul(nptr, &endptr, 10);
149 if (endptr <= nptr)
150 return -EINVAL;
151 if (bts_id)
152 *bts_id = ul & 0xffff;
153
154 if (*endptr++ != '/')
155 return -EINVAL;
156
157 nptr = endptr;
158 ul = strtoul(nptr, &endptr, 10);
159 if (endptr <= nptr)
160 return -EINVAL;
161 if (trx_id)
162 *trx_id = ul & 0xffff;
163
164 return 0;
165}
166
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100167/* send the id ack */
168int ipaccess_send_id_ack(int fd)
169{
170 return write(fd, id_ack, sizeof(id_ack));
171}
172
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100173int ipaccess_send_id_req(int fd)
174{
175 return write(fd, id_req, sizeof(id_req));
176}
177
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200178/* base handling of the ip.access protocol */
179int ipaccess_rcvmsg_base(struct msgb *msg,
180 struct bsc_fd *bfd)
Harald Welteedb37782009-05-01 14:59:07 +0000181{
Harald Welte5fd8a542009-02-13 02:43:36 +0000182 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +0000183 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000184
Harald Welte5fd8a542009-02-13 02:43:36 +0000185 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000186 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000187 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000188 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000189 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000190 DEBUGP(DMI, "PONG!\n");
191 break;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200192 case IPAC_MSGT_ID_ACK:
193 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100194 ret = ipaccess_send_id_ack(bfd->fd);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200195 break;
196 }
197 return 0;
198}
199
200static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
201 struct bsc_fd *bfd)
202{
203 struct tlv_parsed tlvp;
204 u_int8_t msg_type = *(msg->l2h);
205 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
206 struct gsm_bts *bts;
207
208 /* handle base messages */
209 ipaccess_rcvmsg_base(msg, bfd);
210
211 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000212 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000213 DEBUGP(DMI, "ID_RESP ");
214 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +0100215 ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
Harald Welteedb37782009-05-01 14:59:07 +0000216 msgb_l2len(msg)-2);
217 DEBUGP(DMI, "\n");
218
219 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
220 break;
221
222 /* lookup BTS, create sign_link, ... */
223 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
224 &site_id, &bts_id, &trx_id);
225 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
226 if (!bts) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100227 LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for "
Harald Welteedb37782009-05-01 14:59:07 +0000228 " %u/%u/%u, disconnecting\n", site_id, bts_id,
229 trx_id);
230 return -EIO;
231 }
232 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
233 if (bfd->priv_nr == 1) {
234 bts->oml_link = e1inp_sign_link_create(&line->ts[1-1],
235 E1INP_SIGN_OML, bts->c0,
Harald Welte8175e952009-10-20 00:22:00 +0200236 bts->oml_tei, 0);
Harald Welteedb37782009-05-01 14:59:07 +0000237 } else if (bfd->priv_nr == 2) {
238 struct e1inp_ts *e1i_ts;
239 struct bsc_fd *newbfd;
Harald Welte8175e952009-10-20 00:22:00 +0200240 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id);
Harald Welteedb37782009-05-01 14:59:07 +0000241
Harald Welteedb37782009-05-01 14:59:07 +0000242 bfd->data = line = bts->oml_link->ts->line;
Harald Welte8175e952009-10-20 00:22:00 +0200243 e1i_ts = &line->ts[2+trx_id - 1];
Harald Welteedb37782009-05-01 14:59:07 +0000244 newbfd = &e1i_ts->driver.ipaccess.fd;
Harald Welte8175e952009-10-20 00:22:00 +0200245 e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000246
Harald Welte8175e952009-10-20 00:22:00 +0200247 trx->rsl_link = e1inp_sign_link_create(e1i_ts,
248 E1INP_SIGN_RSL, trx,
249 trx->rsl_tei, 0);
Holger Hans Peter Freyther354ef812010-03-24 04:02:55 +0100250
251 if (newbfd->fd >= 0) {
252 LOGP(DINP, LOGL_ERROR, "BTS is still registered. Closing old connection.\n");
253 bsc_unregister_fd(newbfd);
254 close(newbfd->fd);
255 }
256
Harald Welteedb37782009-05-01 14:59:07 +0000257 /* get rid of our old temporary bfd */
258 memcpy(newbfd, bfd, sizeof(*newbfd));
Harald Welte1394fea2009-12-21 23:01:33 +0100259 newbfd->priv_nr = 2+trx_id;
Harald Welteedb37782009-05-01 14:59:07 +0000260 bsc_unregister_fd(bfd);
261 bsc_register_fd(newbfd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200262 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000263 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000264 break;
Harald Welte5fd8a542009-02-13 02:43:36 +0000265 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000266 return 0;
267}
268
Harald Welte88a412a2009-12-16 17:32:37 +0100269#define OML_UP 0x0001
270#define RSL_UP 0x0002
Harald Welte7782c142009-02-15 03:39:51 +0000271
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200272/*
273 * read one ipa message from the socket
274 * return NULL in case of error
275 */
276struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte5fd8a542009-02-13 02:43:36 +0000277{
Harald Welte966636f2009-06-26 19:39:35 +0200278 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000279 struct ipaccess_head *hh;
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100280 int len, ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000281
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200282 if (!msg) {
283 *error = -ENOMEM;
284 return NULL;
285 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000286
287 /* first read our 3-byte header */
288 hh = (struct ipaccess_head *) msg->data;
289 ret = recv(bfd->fd, msg->data, 3, 0);
290 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100291 if (errno != EAGAIN)
292 LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200293 msgb_free(msg);
294 *error = ret;
295 return NULL;
296 } else if (ret == 0) {
297 msgb_free(msg);
298 *error = ret;
299 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000300 }
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200301
Harald Welte5fd8a542009-02-13 02:43:36 +0000302 msgb_put(msg, ret);
303
304 /* then read te length as specified in header */
305 msg->l2h = msg->data + sizeof(*hh);
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100306 len = ntohs(hh->len);
307 ret = recv(bfd->fd, msg->l2h, len, 0);
308 if (ret < len) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100309 LOGP(DINP, LOGL_ERROR, "short read!\n");
Harald Welte5c1e4582009-02-15 11:57:29 +0000310 msgb_free(msg);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200311 *error = -EIO;
312 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000313 }
314 msgb_put(msg, ret);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200315
316 return msg;
317}
318
319static int handle_ts1_read(struct bsc_fd *bfd)
320{
321 struct e1inp_line *line = bfd->data;
322 unsigned int ts_nr = bfd->priv_nr;
323 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
324 struct e1inp_sign_link *link;
325 struct msgb *msg;
326 struct ipaccess_head *hh;
Holger Hans Peter Freyther67b59612009-10-27 10:08:38 +0100327 int ret = 0, error;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200328
329 msg = ipaccess_read_msg(bfd, &error);
330 if (!msg) {
331 if (error == 0) {
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100332 link = e1inp_lookup_sign_link(e1i_ts, IPAC_PROTO_OML, 0);
333 if (link) {
334 link->trx->bts->ip_access.flags = 0;
335 LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n",
336 link->trx->bts->nr);
337 } else
338 LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n");
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200339 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
340 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
341 bsc_unregister_fd(bfd);
342 close(bfd->fd);
343 bfd->fd = -1;
344 }
345 return error;
346 }
347
Sylvain Munaut86538c72009-09-30 22:35:04 +0200348 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000349
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200350 hh = (struct ipaccess_head *) msg->data;
Harald Welteedb37782009-05-01 14:59:07 +0000351 if (hh->proto == IPAC_PROTO_IPACCESS) {
352 ret = ipaccess_rcvmsg(line, msg, bfd);
353 if (ret < 0) {
354 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
355 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
356 bsc_unregister_fd(bfd);
357 close(bfd->fd);
358 bfd->fd = -1;
359 }
360 msgb_free(msg);
361 return ret;
362 }
363 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
364 * might have free'd it !!! */
365
Harald Welte8175e952009-10-20 00:22:00 +0200366 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
Harald Welte5fd8a542009-02-13 02:43:36 +0000367 if (!link) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100368 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
369 "hh->proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000370 msgb_free(msg);
371 return -EIO;
372 }
373 msg->trx = link->trx;
374
Harald Welte8175e952009-10-20 00:22:00 +0200375 switch (link->type) {
376 case E1INP_SIGN_RSL:
Harald Welte1394fea2009-12-21 23:01:33 +0100377 if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) {
Harald Weltee73501a2009-10-21 21:16:00 +0200378 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte1394fea2009-12-21 23:01:33 +0100379 msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr);
Harald Weltee73501a2009-10-21 21:16:00 +0200380 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000381 ret = abis_rsl_rcvmsg(msg);
382 break;
Harald Welte8175e952009-10-20 00:22:00 +0200383 case E1INP_SIGN_OML:
Harald Welte88a412a2009-12-16 17:32:37 +0100384 if (!(msg->trx->bts->ip_access.flags & OML_UP)) {
Harald Welte8175e952009-10-20 00:22:00 +0200385 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte88a412a2009-12-16 17:32:37 +0100386 msg->trx->bts->ip_access.flags |= OML_UP;
Harald Welte7782c142009-02-15 03:39:51 +0000387 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000388 ret = abis_nm_rcvmsg(msg);
389 break;
390 default:
Harald Welteafdca0f2009-12-23 23:01:04 +0100391 LOGP(DINP, LOGL_NOTICE, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000392 msgb_free(msg);
393 break;
394 }
395 return ret;
396}
397
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200398void ipaccess_prepend_header(struct msgb *msg, int proto)
399{
400 struct ipaccess_head *hh;
401
402 /* prepend the ip.access header */
403 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100404 hh->len = htons(msg->len - sizeof(*hh));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200405 hh->proto = proto;
406}
407
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200408static int ts_want_write(struct e1inp_ts *e1i_ts)
409{
410 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
411
412 return 0;
413}
414
415static void timeout_ts1_write(void *data)
416{
417 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
418
419 /* trigger write of ts1, due to tx delay timer */
420 ts_want_write(e1i_ts);
421}
422
Harald Welte5fd8a542009-02-13 02:43:36 +0000423static int handle_ts1_write(struct bsc_fd *bfd)
424{
425 struct e1inp_line *line = bfd->data;
426 unsigned int ts_nr = bfd->priv_nr;
427 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
428 struct e1inp_sign_link *sign_link;
429 struct msgb *msg;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200430 u_int8_t proto;
Harald Welte5fd8a542009-02-13 02:43:36 +0000431 int ret;
432
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200433 bfd->when &= ~BSC_FD_WRITE;
434
Harald Welte5fd8a542009-02-13 02:43:36 +0000435 /* get the next msg for this timeslot */
436 msg = e1inp_tx_ts(e1i_ts, &sign_link);
437 if (!msg) {
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200438 /* no message after tx delay timer */
Harald Welte5fd8a542009-02-13 02:43:36 +0000439 return 0;
440 }
441
Harald Welte5fd8a542009-02-13 02:43:36 +0000442 switch (sign_link->type) {
443 case E1INP_SIGN_OML:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200444 proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000445 break;
446 case E1INP_SIGN_RSL:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200447 proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000448 break;
449 default:
450 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200451 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Harald Welte5fd8a542009-02-13 02:43:36 +0000452 return -EINVAL;
453 }
454
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200455 msg->l2h = msg->data;
Harald Welte8175e952009-10-20 00:22:00 +0200456 ipaccess_prepend_header(msg, sign_link->tei);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200457
458 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000459
460 ret = send(bfd->fd, msg->data, msg->len, 0);
461 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200462
463 /* set tx delay timer for next event */
464 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
465 e1i_ts->sign.tx_timer.data = e1i_ts;
466 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000467
468 return ret;
469}
470
Harald Welte5fd8a542009-02-13 02:43:36 +0000471/* callback from select.c in case one of the fd's can be read/written */
472static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
473{
474 struct e1inp_line *line = bfd->data;
475 unsigned int ts_nr = bfd->priv_nr;
476 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000477 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000478 int rc = 0;
479
Harald Welteedb37782009-05-01 14:59:07 +0000480 /* In case of early RSL we might not yet have a line */
481
482 if (line)
483 e1i_ts = &line->ts[idx];
484
485 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000486 if (what & BSC_FD_READ)
487 rc = handle_ts1_read(bfd);
488 if (what & BSC_FD_WRITE)
489 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000490 } else
Harald Welteafdca0f2009-12-23 23:01:04 +0100491 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000492
493 return rc;
494}
495
496
Harald Welte5fd8a542009-02-13 02:43:36 +0000497struct e1inp_driver ipaccess_driver = {
498 .name = "ip.access",
499 .want_write = ts_want_write,
500};
501
Harald Welteedb37782009-05-01 14:59:07 +0000502/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000503static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
504{
Harald Welte5fd8a542009-02-13 02:43:36 +0000505 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000506 int idx = 0;
507 struct e1inp_line *line;
508 struct e1inp_ts *e1i_ts;
509 struct bsc_fd *bfd;
510 struct sockaddr_in sa;
511 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000512
Harald Welteedb37782009-05-01 14:59:07 +0000513 if (!(what & BSC_FD_READ))
514 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000515
Harald Welteedb37782009-05-01 14:59:07 +0000516 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
517 if (ret < 0) {
518 perror("accept");
519 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000520 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100521 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
522 inet_ntoa(sa.sin_addr));
Harald Welteedb37782009-05-01 14:59:07 +0000523
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100524 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000525 if (!line) {
526 close(ret);
527 return -ENOMEM;
528 }
Harald Welteedb37782009-05-01 14:59:07 +0000529 line->driver = &ipaccess_driver;
530 //line->driver_data = e1h;
531 /* create virrtual E1 timeslots for signalling */
532 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000533
534 e1i_ts = &line->ts[idx];
535
536 bfd = &e1i_ts->driver.ipaccess.fd;
537 bfd->fd = ret;
538 bfd->data = line;
539 bfd->priv_nr = 1;
540 bfd->cb = ipaccess_fd_cb;
541 bfd->when = BSC_FD_READ;
542 ret = bsc_register_fd(bfd);
543 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100544 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000545 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200546 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000547 return ret;
548 }
549
550 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100551 ret = ipaccess_send_id_req(bfd->fd);
Harald Welteedb37782009-05-01 14:59:07 +0000552
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200553 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200554 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000555}
556
Harald Welte5c1e4582009-02-15 11:57:29 +0000557static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
558{
Harald Welteedb37782009-05-01 14:59:07 +0000559 struct sockaddr_in sa;
560 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200561 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000562 int ret;
563
Harald Welteedb37782009-05-01 14:59:07 +0000564 if (!(what & BSC_FD_READ))
565 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000566
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100567 bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200568 if (!bfd)
569 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200570
Harald Welteedb37782009-05-01 14:59:07 +0000571 /* Some BTS has connected to us, but we don't know yet which line
572 * (as created by the OML link) to associate it with. Thus, we
Holger Hans Peter Freytherc7df7c62009-11-15 13:50:39 +0100573 * allocate a temporary bfd until we have received ID from BTS */
Harald Welteedb37782009-05-01 14:59:07 +0000574
575 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
576 if (bfd->fd < 0) {
577 perror("accept");
578 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000579 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100580 LOGP(DINP, LOGL_NOTICE, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
Harald Welteedb37782009-05-01 14:59:07 +0000581 bfd->priv_nr = 2;
582 bfd->cb = ipaccess_fd_cb;
583 bfd->when = BSC_FD_READ;
584 ret = bsc_register_fd(bfd);
585 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100586 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000587 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200588 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000589 return ret;
590 }
591 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
592 ret = write(bfd->fd, id_req, sizeof(id_req));
593
Harald Welte5c1e4582009-02-15 11:57:29 +0000594 return 0;
595}
596
597static int make_sock(struct bsc_fd *bfd, u_int16_t port,
Harald Welte5c1e4582009-02-15 11:57:29 +0000598 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000599{
600 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000601 int ret, on = 1;
602
603 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
604 bfd->cb = cb;
605 bfd->when = BSC_FD_READ;
Harald Welteedb37782009-05-01 14:59:07 +0000606 //bfd->data = line;
Harald Welte5c1e4582009-02-15 11:57:29 +0000607
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100608 if (bfd->fd < 0) {
609 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
610 return -EIO;
611 }
612
Harald Welte5c1e4582009-02-15 11:57:29 +0000613 memset(&addr, 0, sizeof(addr));
614 addr.sin_family = AF_INET;
615 addr.sin_port = htons(port);
616 addr.sin_addr.s_addr = INADDR_ANY;
617
618 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
619
620 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
621 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100622 LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n",
Harald Welte5c1e4582009-02-15 11:57:29 +0000623 strerror(errno));
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100624 close(bfd->fd);
Harald Welte5c1e4582009-02-15 11:57:29 +0000625 return -EIO;
626 }
627
628 ret = listen(bfd->fd, 1);
629 if (ret < 0) {
630 perror("listen");
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100631 close(bfd->fd);
Harald Welte5c1e4582009-02-15 11:57:29 +0000632 return ret;
633 }
634
635 ret = bsc_register_fd(bfd);
636 if (ret < 0) {
637 perror("register_listen_fd");
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100638 close(bfd->fd);
Harald Welte5c1e4582009-02-15 11:57:29 +0000639 return ret;
640 }
641 return 0;
642}
643
Harald Welte25de9912009-04-30 15:53:07 +0000644/* Actively connect to a BTS. Currently used by ipaccess-config.c */
645int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
646{
647 struct e1inp_ts *e1i_ts = &line->ts[0];
648 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
649 int ret, on = 1;
650
651 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
652 bfd->cb = ipaccess_fd_cb;
653 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
654 bfd->data = line;
655 bfd->priv_nr = 1;
656
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100657 if (bfd->fd < 0) {
658 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
659 return -EIO;
660 }
661
Harald Welte25de9912009-04-30 15:53:07 +0000662 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
663
664 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
665 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100666 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
Harald Welte25de9912009-04-30 15:53:07 +0000667 close(bfd->fd);
668 return ret;
669 }
670
671 ret = bsc_register_fd(bfd);
672 if (ret < 0) {
673 close(bfd->fd);
674 return ret;
675 }
676
677 line->driver = &ipaccess_driver;
678
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200679 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200680 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000681}
682
Harald Welteedb37782009-05-01 14:59:07 +0000683int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000684{
Harald Welte5c1e4582009-02-15 11:57:29 +0000685 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000686
687 /* register the driver with the core */
688 /* FIXME: do this in the plugin initializer function */
689 ret = e1inp_driver_register(&ipaccess_driver);
690 if (ret)
691 return ret;
692
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100693 e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200694 if (!e1h)
695 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200696
Harald Welteedb37782009-05-01 14:59:07 +0000697 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000698
Harald Welte5c1e4582009-02-15 11:57:29 +0000699 /* Listen for OML connections */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100700 ret = make_sock(&e1h->listen_fd, IPA_TCP_PORT_OML, listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000701 if (ret < 0)
702 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000703
Harald Welte5c1e4582009-02-15 11:57:29 +0000704 /* Listen for RSL connections */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100705 ret = make_sock(&e1h->rsl_listen_fd, IPA_TCP_PORT_RSL, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000706
Harald Welteedb37782009-05-01 14:59:07 +0000707 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000708}