Harald Welte | 5fd8a54 | 2009-02-13 02:43:36 +0000 | [diff] [blame^] | 1 | /* 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 | |
| 35 | #include <openbsc/select.h> |
| 36 | #include <openbsc/msgb.h> |
| 37 | #include <openbsc/debug.h> |
| 38 | #include <openbsc/gsm_data.h> |
| 39 | #include <openbsc/abis_nm.h> |
| 40 | #include <openbsc/abis_rsl.h> |
| 41 | #include <openbsc/subchan_demux.h> |
| 42 | #include <openbsc/e1_input.h> |
| 43 | |
| 44 | /* data structure for one E1 interface with A-bis */ |
| 45 | struct ia_e1_handle { |
| 46 | struct bsc_fd listen_fd; |
| 47 | }; |
| 48 | |
| 49 | #define TS1_ALLOC_SIZE 300 |
| 50 | |
| 51 | struct ipaccess_head { |
| 52 | u_int8_t zero; |
| 53 | u_int8_t len; |
| 54 | u_int8_t proto; |
| 55 | u_int8_t data[0]; |
| 56 | } __attribute__ ((packed)); |
| 57 | |
| 58 | enum ipaccess_proto { |
| 59 | PROTO_RSL = 0x00, |
| 60 | PROTO_IPACCESS = 0xfe, |
| 61 | PROTO_OML = 0xff, |
| 62 | }; |
| 63 | |
| 64 | enum ipaccess_msg_type { |
| 65 | MSGT_PING = 0x00, |
| 66 | MSGT_PONG = 0x01, |
| 67 | MSGT_IDENTITY_GET = 0x04, |
| 68 | MSGT_IDENTITY_RESP = 0x05, |
| 69 | MSGT_IDENTITY_ACK = 0x06, |
| 70 | }; |
| 71 | |
| 72 | static const u_int8_t pong[] = { 0, 1, PROTO_IPACCESS, MSGT_PONG }; |
| 73 | static const u_int8_t id_ack[] = { 0, 1, PROTO_IPACCESS, MSGT_IDENTITY_ACK }; |
| 74 | |
| 75 | static int ipaccess_rcvmsg(struct msgb *msg, int fd) |
| 76 | { |
| 77 | u_int8_t msg_type = *(msg->l2h); |
| 78 | |
| 79 | printf("msg_type=0x%02x\n", msg_type); |
| 80 | switch (msg_type) { |
| 81 | case MSGT_PING: |
| 82 | DEBUGP(DMI, "PING?\n"); |
| 83 | write(fd, pong, sizeof(pong)); |
| 84 | break; |
| 85 | case MSGT_PONG: |
| 86 | DEBUGP(DMI, "PONG!\n"); |
| 87 | break; |
| 88 | case MSGT_IDENTITY_RESP: |
| 89 | DEBUGP(DMI, "ID_RESP\n"); |
| 90 | break; |
| 91 | case MSGT_IDENTITY_ACK: |
| 92 | DEBUGP(DMI, "ID_ACK\n"); |
| 93 | write(fd, id_ack, sizeof(id_ack)); |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | msgb_free(msg); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 102 | { |
| 103 | struct e1inp_line *line = bfd->data; |
| 104 | unsigned int ts_nr = bfd->priv_nr; |
| 105 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 106 | struct e1inp_sign_link *link; |
| 107 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE); |
| 108 | struct ipaccess_head *hh; |
| 109 | int ret; |
| 110 | |
| 111 | if (!msg) |
| 112 | return -ENOMEM; |
| 113 | |
| 114 | /* first read our 3-byte header */ |
| 115 | hh = (struct ipaccess_head *) msg->data; |
| 116 | ret = recv(bfd->fd, msg->data, 3, 0); |
| 117 | if (ret < 0) { |
| 118 | fprintf(stderr, "recv error %s\n", strerror(errno)); |
| 119 | return ret; |
| 120 | } |
| 121 | if (ret == 0) { |
| 122 | fprintf(stderr, "BTS disappeared, dead socket\n"); |
| 123 | bsc_unregister_fd(bfd); |
| 124 | close(bfd->fd); |
| 125 | bfd->fd = -1; |
| 126 | } |
| 127 | msgb_put(msg, ret); |
| 128 | |
| 129 | /* then read te length as specified in header */ |
| 130 | msg->l2h = msg->data + sizeof(*hh); |
| 131 | ret = recv(bfd->fd, msg->l2h, hh->len, 0); |
| 132 | if (ret < hh->len) { |
| 133 | fprintf(stderr, "short read!\n"); |
| 134 | //msgb_free(msg); |
| 135 | //return -EIO; |
| 136 | } |
| 137 | msgb_put(msg, ret); |
| 138 | DEBUGP(DMI, "<= ret=%d, len=%d, proto=0x%02x\n", ret, hh->len, hh->proto); |
| 139 | |
| 140 | if (hh->proto == PROTO_IPACCESS) |
| 141 | return ipaccess_rcvmsg(msg, bfd->fd); |
| 142 | |
| 143 | if (debug_mask & DMI) { |
| 144 | fprintf(stdout, "RX: "); |
| 145 | hexdump(msgb_l2(msg), ret); |
| 146 | } |
| 147 | |
| 148 | link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto); |
| 149 | if (!link) { |
| 150 | printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto); |
| 151 | msgb_free(msg); |
| 152 | return -EIO; |
| 153 | } |
| 154 | msg->trx = link->trx; |
| 155 | |
| 156 | switch (hh->proto) { |
| 157 | case PROTO_RSL: |
| 158 | ret = abis_rsl_rcvmsg(msg); |
| 159 | break; |
| 160 | case PROTO_OML: |
| 161 | ret = abis_nm_rcvmsg(msg); |
| 162 | break; |
| 163 | default: |
| 164 | msgb_free(msg); |
| 165 | break; |
| 166 | } |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 171 | { |
| 172 | struct e1inp_line *line = bfd->data; |
| 173 | unsigned int ts_nr = bfd->priv_nr; |
| 174 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 175 | struct e1inp_sign_link *sign_link; |
| 176 | struct msgb *msg; |
| 177 | struct ipaccess_head *hh; |
| 178 | u_int8_t *l2_data; |
| 179 | int ret; |
| 180 | |
| 181 | /* get the next msg for this timeslot */ |
| 182 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 183 | if (!msg) { |
| 184 | bfd->when &= ~BSC_FD_WRITE; |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | l2_data = msg->data; |
| 189 | |
| 190 | /* prepend the mISDNhead */ |
| 191 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
| 192 | hh->zero = 0; |
| 193 | hh->len = msg->len - sizeof(*hh); |
| 194 | |
| 195 | switch (sign_link->type) { |
| 196 | case E1INP_SIGN_OML: |
| 197 | hh->proto = PROTO_OML; |
| 198 | break; |
| 199 | case E1INP_SIGN_RSL: |
| 200 | hh->proto = PROTO_RSL; |
| 201 | break; |
| 202 | default: |
| 203 | msgb_free(msg); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | if (debug_mask & DMI) { |
| 208 | fprintf(stdout, "TX proto=0x%x: ", hh->proto); |
| 209 | hexdump(l2_data, hh->len); |
| 210 | } |
| 211 | |
| 212 | ret = send(bfd->fd, msg->data, msg->len, 0); |
| 213 | msgb_free(msg); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /* callback from select.c in case one of the fd's can be read/written */ |
| 220 | static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 221 | { |
| 222 | struct e1inp_line *line = bfd->data; |
| 223 | unsigned int ts_nr = bfd->priv_nr; |
| 224 | unsigned int idx = ts_nr-1; |
| 225 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 226 | int rc = 0; |
| 227 | |
| 228 | switch (e1i_ts->type) { |
| 229 | case E1INP_TS_TYPE_SIGN: |
| 230 | if (what & BSC_FD_READ) |
| 231 | rc = handle_ts1_read(bfd); |
| 232 | if (what & BSC_FD_WRITE) |
| 233 | rc = handle_ts1_write(bfd); |
| 234 | break; |
| 235 | #if 0 |
| 236 | case E1INP_TS_TYPE_TRAU: |
| 237 | if (what & BSC_FD_READ) |
| 238 | rc = handle_tsX_read(bfd); |
| 239 | /* We never include the mISDN B-Channel FD into the |
| 240 | * writeset, since it doesn't support poll() based |
| 241 | * write flow control */ |
| 242 | break; |
| 243 | #endif |
| 244 | default: |
| 245 | fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type); |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | return rc; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 254 | { |
| 255 | /* We never include the mISDN B-Channel FD into the |
| 256 | * writeset, since it doesn't support poll() based |
| 257 | * write flow control */ |
| 258 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) |
| 259 | return 0; |
| 260 | |
| 261 | e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE; |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | struct e1inp_driver ipaccess_driver = { |
| 267 | .name = "ip.access", |
| 268 | .want_write = ts_want_write, |
| 269 | }; |
| 270 | |
| 271 | static int ia_e1_setup(struct e1inp_line *line) |
| 272 | { |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what) |
| 277 | { |
| 278 | struct e1inp_line *line = listen_bfd->data; |
| 279 | int ret; |
| 280 | |
| 281 | if (what & BSC_FD_READ) { |
| 282 | int idx = 0; |
| 283 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 284 | struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd; |
| 285 | struct sockaddr_in sa; |
| 286 | socklen_t sa_len = sizeof(sa); |
| 287 | |
| 288 | if (bfd->fd) { |
| 289 | printf("dumping old fd\n"); |
| 290 | if (bfd->fd != -1) { |
| 291 | bsc_unregister_fd(bfd); |
| 292 | close(bfd->fd); |
| 293 | } |
| 294 | } |
| 295 | bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 296 | if (bfd->fd < 0) { |
| 297 | perror("accept"); |
| 298 | return bfd->fd; |
| 299 | } |
| 300 | printf("accept()ed new RSL/OML fd\n"); |
| 301 | bfd->data = line; |
| 302 | bfd->priv_nr = 1; |
| 303 | bfd->cb = ipaccess_fd_cb; |
| 304 | bfd->when = BSC_FD_READ; |
| 305 | ret = bsc_register_fd(bfd); |
| 306 | if (ret < 0) { |
| 307 | fprintf(stderr, "could not register FD\n"); |
| 308 | return ret; |
| 309 | } |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | int ipaccess_setup(struct e1inp_line *line) |
| 315 | { |
| 316 | struct sockaddr_in addr; |
| 317 | struct ia_e1_handle *e1h; |
| 318 | int sk, ret, on = 1; |
| 319 | |
| 320 | /* register the driver with the core */ |
| 321 | /* FIXME: do this in the plugin initializer function */ |
| 322 | ret = e1inp_driver_register(&ipaccess_driver); |
| 323 | if (ret) |
| 324 | return ret; |
| 325 | |
| 326 | /* create the actual line instance */ |
| 327 | /* FIXME: do this independent of driver registration */ |
| 328 | e1h = malloc(sizeof(*e1h)); |
| 329 | memset(e1h, 0, sizeof(*e1h)); |
| 330 | |
| 331 | line->driver = &ipaccess_driver; |
| 332 | line->driver_data = e1h; |
| 333 | |
| 334 | e1h->listen_fd.fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 335 | e1h->listen_fd.cb = listen_fd_cb; |
| 336 | e1h->listen_fd.when = BSC_FD_READ; |
| 337 | e1h->listen_fd.data = line; |
| 338 | |
| 339 | memset(&addr, 0, sizeof(addr)); |
| 340 | addr.sin_family = AF_INET; |
| 341 | addr.sin_port = htons(3002); |
| 342 | addr.sin_addr.s_addr = INADDR_ANY; |
| 343 | |
| 344 | setsockopt(e1h->listen_fd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 345 | |
| 346 | ret = bind(e1h->listen_fd.fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 347 | if (ret < 0) { |
| 348 | fprintf(stderr, "could not bind l2 socket %s\n", |
| 349 | strerror(errno)); |
| 350 | return -EIO; |
| 351 | } |
| 352 | |
| 353 | ret = listen(e1h->listen_fd.fd, 1); |
| 354 | if (ret < 0) { |
| 355 | perror("listen"); |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | ret = bsc_register_fd(&e1h->listen_fd); |
| 360 | if (ret < 0) { |
| 361 | perror("register_listen_fd"); |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | ret = ia_e1_setup(line); |
| 366 | if (ret) |
| 367 | return ret; |
| 368 | |
| 369 | return e1inp_line_register(line); |
| 370 | } |