blob: c8ee7812daaf512a58521992843470842b8a07a3 [file] [log] [blame]
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001#include "internal.h"
2
3#include <stdio.h>
4#include <unistd.h>
5#include <stdlib.h>
6#include <errno.h>
7#include <string.h>
8#include <time.h>
9#include <sys/fcntl.h>
10#include <sys/socket.h>
11#include <sys/ioctl.h>
12#include <arpa/inet.h>
13
14#include <osmocom/core/select.h>
15#include <osmocom/gsm/tlv.h>
16#include <osmocom/core/msgb.h>
17#include <osmocom/core/logging.h>
18#include <talloc.h>
19#include <osmocom/abis/e1_input.h>
20#include <osmocom/abis/ipaccess.h>
21#include <osmocom/core/socket.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020022
23#include <osmocom/abis/ipa.h>
24
25#define IPA_ALLOC_SIZE 1200
26
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +020027struct msgb *ipa_msg_alloc(int headroom)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020028{
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +020029 struct msgb *nmsg;
30
31 headroom += sizeof(struct ipaccess_head);
32
33 nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "Abis/IP");
34 if (!nmsg)
35 return NULL;
36 return nmsg;
37}
38
39void ipa_msg_push_header(struct msgb *msg, uint8_t proto)
40{
41 struct ipaccess_head *hh;
42
43 msg->l2h = msg->data;
44 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
45 hh->proto = proto;
46 hh->len = htons(msgb_l2len(msg));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020047}
48
49int ipa_msg_recv(int fd, struct msgb **rmsg)
50{
51 struct msgb *msg;
52 struct ipaccess_head *hh;
53 int len, ret;
54
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +020055 msg = ipa_msg_alloc(0);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020056 if (msg == NULL)
57 return -ENOMEM;
58
59 /* first read our 3-byte header */
60 hh = (struct ipaccess_head *) msg->data;
61 ret = recv(fd, msg->data, sizeof(*hh), 0);
62 if (ret <= 0) {
63 msgb_free(msg);
64 return ret;
65 } else if (ret != sizeof(*hh)) {
66 msgb_free(msg);
67 return -EIO;
68 }
69 msgb_put(msg, ret);
70
71 /* then read the length as specified in header */
72 msg->l2h = msg->data + sizeof(*hh);
73 len = ntohs(hh->len);
74
75 if (len < 0 || IPA_ALLOC_SIZE < len + sizeof(*hh)) {
76 msgb_free(msg);
77 return -EIO;
78 }
79
80 ret = recv(fd, msg->l2h, len, 0);
81 if (ret <= 0) {
82 msgb_free(msg);
83 return ret;
84 } else if (ret < len) {
85 msgb_free(msg);
86 return -EIO;
87 }
88 msgb_put(msg, ret);
89 *rmsg = msg;
90 return ret;
91}
92
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +020093void ipa_client_link_close(struct ipa_client_link *link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020094
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +020095static void ipa_client_retry(struct ipa_client_link *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020096{
97 LOGP(DINP, LOGL_NOTICE, "connection closed\n");
98 ipa_client_link_close(link);
99 LOGP(DINP, LOGL_NOTICE, "retrying in 5 seconds...\n");
100 osmo_timer_schedule(&link->timer, 5, 0);
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200101 link->state = IPA_CLIENT_LINK_STATE_CONNECTING;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200102}
103
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200104void ipa_client_link_close(struct ipa_client_link *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200105{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200106 osmo_fd_unregister(link->ofd);
107 close(link->ofd->fd);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200108}
109
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200110static void ipa_client_read(struct ipa_client_link *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200111{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200112 struct osmo_fd *ofd = link->ofd;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200113 struct msgb *msg;
114 int ret;
115
116 LOGP(DINP, LOGL_NOTICE, "message received\n");
117
118 ret = ipa_msg_recv(ofd->fd, &msg);
119 if (ret < 0) {
120 if (errno == EPIPE || errno == ECONNRESET) {
121 LOGP(DINP, LOGL_ERROR, "lost connection with server\n");
122 } else {
123 LOGP(DINP, LOGL_ERROR, "unknown error\n");
124 }
125 ipa_client_retry(link);
126 return;
127 } else if (ret == 0) {
128 LOGP(DINP, LOGL_ERROR, "connection closed with server\n");
129 ipa_client_retry(link);
130 return;
131 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200132 if (link->read_cb)
133 link->read_cb(link, msg);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200134}
135
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200136static void ipa_client_write(struct ipa_client_link *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200137{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200138 if (link->write_cb)
139 link->write_cb(link);
140}
141
142int ipa_client_write_default_cb(struct ipa_client_link *link)
143{
144 struct osmo_fd *ofd = link->ofd;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200145 struct msgb *msg;
146 struct llist_head *lh;
147 int ret;
148
149 LOGP(DINP, LOGL_NOTICE, "sending data\n");
150
151 if (llist_empty(&link->tx_queue)) {
152 ofd->when &= ~BSC_FD_WRITE;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200153 return 0;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200154 }
155 lh = link->tx_queue.next;
156 llist_del(lh);
157 msg = llist_entry(lh, struct msgb, list);
158
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200159 ret = send(link->ofd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200160 if (ret < 0) {
161 if (errno == EPIPE || errno == ENOTCONN) {
162 ipa_client_retry(link);
163 }
164 LOGP(DINP, LOGL_ERROR, "error to send\n");
165 }
166 msgb_free(msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200167 return 0;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200168}
169
170int ipa_client_fd_cb(struct osmo_fd *ofd, unsigned int what)
171{
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200172 struct ipa_client_link *link = ofd->data;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200173 int error, ret;
174 size_t len = sizeof(error);
175
176 switch(link->state) {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200177 case IPA_CLIENT_LINK_STATE_CONNECTING:
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200178 ret = getsockopt(ofd->fd, SOL_SOCKET, SO_ERROR, &error, &len);
179 if (ret >= 0 && error > 0) {
180 ipa_client_retry(link);
181 return 0;
182 }
183 ofd->when &= ~BSC_FD_WRITE;
184 LOGP(DINP, LOGL_NOTICE, "connection done.\n");
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200185 link->state = IPA_CLIENT_LINK_STATE_CONNECTED;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200186 if (link->connect_cb)
187 link->connect_cb(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200188 break;
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200189 case IPA_CLIENT_LINK_STATE_CONNECTED:
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200190 if (what & BSC_FD_READ) {
191 LOGP(DINP, LOGL_NOTICE, "connected read\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200192 ipa_client_read(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200193 }
194 if (what & BSC_FD_WRITE) {
195 LOGP(DINP, LOGL_NOTICE, "connected write\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200196 ipa_client_write(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200197 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200198 break;
199 default:
200 break;
201 }
202 return 0;
203}
204
205static void ipa_link_timer_cb(void *data);
206
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200207struct ipa_client_link *
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200208ipa_client_link_create(void *ctx, struct e1inp_ts *ts, const char *driver_name,
209 int priv_nr, const char *addr, uint16_t port,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200210 int (*connect_cb)(struct ipa_client_link *link),
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200211 int (*read_cb)(struct ipa_client_link *link,
212 struct msgb *msgb),
213 int (*write_cb)(struct ipa_client_link *link),
214 void *data)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200215{
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200216 struct ipa_client_link *ipa_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200217
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200218 ipa_link = talloc_zero(ctx, struct ipa_client_link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200219 if (!ipa_link)
220 return NULL;
221
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200222 if (ts) {
223 struct e1inp_driver *driver;
224
225 driver = e1inp_driver_find(driver_name);
226 if (driver == NULL) {
227 talloc_free(ipa_link);
228 return NULL;
229 }
230 ts->line->driver = driver;
231 ipa_link->ofd = &ts->driver.ipaccess.fd;
232 } else {
233 ipa_link->ofd = talloc_zero(ctx, struct osmo_fd);
234 if (ipa_link->ofd == NULL) {
235 talloc_free(ipa_link);
236 return NULL;
237 }
238 }
239
240 ipa_link->ofd->when |= BSC_FD_READ | BSC_FD_WRITE;
241 ipa_link->ofd->priv_nr = priv_nr;
242 ipa_link->ofd->cb = ipa_client_fd_cb;
243 ipa_link->ofd->data = ipa_link;
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200244 ipa_link->state = IPA_CLIENT_LINK_STATE_CONNECTING;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200245 ipa_link->timer.cb = ipa_link_timer_cb;
246 ipa_link->timer.data = ipa_link;
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200247 ipa_link->addr = talloc_strdup(ipa_link, addr);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200248 ipa_link->port = port;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200249 ipa_link->connect_cb = connect_cb;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250 ipa_link->read_cb = read_cb;
251 ipa_link->write_cb = write_cb;
252 ipa_link->line = ts->line;
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200253 ipa_link->data = data;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200254 INIT_LLIST_HEAD(&ipa_link->tx_queue);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200255
256 return ipa_link;
257}
258
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200259void ipa_client_link_destroy(struct ipa_client_link *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200260{
261 talloc_free(link);
262}
263
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200264int ipa_client_link_open(struct ipa_client_link *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200265{
266 int ret;
267
268 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200269 link->addr, link->port,
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200270 OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK);
271 if (ret < 0) {
272 if (errno != EINPROGRESS)
273 return ret;
274 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200275 link->ofd->fd = ret;
276 if (osmo_fd_register(link->ofd) < 0) {
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200277 close(ret);
278 return -EIO;
279 }
280 return 0;
281}
282
283static void ipa_link_timer_cb(void *data)
284{
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200285 struct ipa_client_link *link = data;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200286
287 LOGP(DINP, LOGL_NOTICE, "reconnecting.\n");
288
289 switch(link->state) {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200290 case IPA_CLIENT_LINK_STATE_CONNECTING:
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200291 ipa_client_link_open(link);
292 break;
293 default:
294 break;
295 }
296}
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200297
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200298void ipa_client_link_send(struct ipa_client_link *link, struct msgb *msg)
299{
300 msgb_enqueue(&link->tx_queue, msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200301 link->ofd->when |= BSC_FD_WRITE;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200302}
303
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200304int ipa_server_fd_cb(struct osmo_fd *ofd, unsigned int what)
305{
306 int ret;
307 struct sockaddr_in sa;
308 socklen_t sa_len = sizeof(sa);
309 struct ipa_server_link *link = ofd->data;
310
311 ret = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len);
312 if (ret < 0) {
313 LOGP(DINP, LOGL_ERROR, "failed to accept from origin "
314 "peer, reason=`%s'\n", strerror(errno));
315 return ret;
316 }
317 LOGP(DINP, LOGL_NOTICE, "accept()ed new link from %s to port %u\n",
318 inet_ntoa(sa.sin_addr), link->port);
319
320 if (link->accept_cb)
321 link->accept_cb(link, ret);
322
323 return 0;
324}
325
326struct ipa_server_link *
327ipa_server_link_create(void *ctx, struct e1inp_line *line,
328 const char *addr, uint16_t port,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200329 int (*accept_cb)(struct ipa_server_link *link, int fd),
330 void *data)
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200331{
332 struct ipa_server_link *ipa_link;
333
334 ipa_link = talloc_zero(ctx, struct ipa_server_link);
335 if (!ipa_link)
336 return NULL;
337
338 ipa_link->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
339 ipa_link->ofd.cb = ipa_server_fd_cb;
340 ipa_link->ofd.data = ipa_link;
341 ipa_link->addr = talloc_strdup(ipa_link, addr);
342 ipa_link->port = port;
343 ipa_link->accept_cb = accept_cb;
344 ipa_link->line = line;
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200345 ipa_link->data = data;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200346
347 return ipa_link;
348
349}
350
351void ipa_server_link_destroy(struct ipa_server_link *link)
352{
353 talloc_free(link);
354}
355
356int ipa_server_link_open(struct ipa_server_link *link)
357{
358 int ret;
359
360 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
361 link->addr, link->port, OSMO_SOCK_F_BIND);
362 if (ret < 0)
363 return ret;
364
365 link->ofd.fd = ret;
366 if (osmo_fd_register(&link->ofd) < 0) {
367 close(ret);
368 return -EIO;
369 }
370 return 0;
371}
372
373void ipa_server_link_close(struct ipa_server_link *link)
374{
375 osmo_fd_unregister(&link->ofd);
376 close(link->ofd.fd);
377}
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200378
379static void ipa_server_peer_read(struct ipa_server_peer *peer)
380{
381 struct osmo_fd *ofd = &peer->ofd;
382 struct msgb *msg;
383 int ret;
384
385 LOGP(DINP, LOGL_NOTICE, "message received\n");
386
387 ret = ipa_msg_recv(ofd->fd, &msg);
388 if (ret < 0) {
389 if (errno == EPIPE || errno == ECONNRESET) {
390 LOGP(DINP, LOGL_ERROR, "lost connection with server\n");
391 } else {
392 LOGP(DINP, LOGL_ERROR, "unknown error\n");
393 }
394 return;
395 } else if (ret == 0) {
396 LOGP(DINP, LOGL_ERROR, "connection closed with server\n");
397 ipa_server_peer_destroy(peer);
398 return;
399 }
400 if (peer->cb)
401 peer->cb(peer, msg);
402
403 return;
404}
405
406static void ipa_server_peer_write(struct ipa_server_peer *peer)
407{
408 struct osmo_fd *ofd = &peer->ofd;
409 struct msgb *msg;
410 struct llist_head *lh;
411 int ret;
412
413 LOGP(DINP, LOGL_NOTICE, "sending data\n");
414
415 if (llist_empty(&peer->tx_queue)) {
416 ofd->when &= ~BSC_FD_WRITE;
417 return;
418 }
419 lh = peer->tx_queue.next;
420 llist_del(lh);
421 msg = llist_entry(lh, struct msgb, list);
422
423 ret = send(peer->ofd.fd, msg->data, msg->len, 0);
424 if (ret < 0) {
425 LOGP(DINP, LOGL_ERROR, "error to send\n");
426 }
427 msgb_free(msg);
428}
429
430static int ipa_server_peer_cb(struct osmo_fd *ofd, unsigned int what)
431{
432 struct ipa_server_peer *peer = ofd->data;
433
434 LOGP(DINP, LOGL_NOTICE, "connected read/write\n");
435 if (what & BSC_FD_READ)
436 ipa_server_peer_read(peer);
437 if (what & BSC_FD_WRITE)
438 ipa_server_peer_write(peer);
439
440 return 0;
441}
442
443struct ipa_server_peer *
444ipa_server_peer_create(void *ctx, struct ipa_server_link *link, int fd,
445 int (*cb)(struct ipa_server_peer *peer, struct msgb *msg),
446 void *data)
447{
448 struct ipa_server_peer *peer;
449
450 peer = talloc_zero(ctx, struct ipa_server_peer);
451 if (peer == NULL) {
452 LOGP(DINP, LOGL_ERROR, "cannot allocate new peer in server, "
453 "reason=`%s'\n", strerror(errno));
454 return NULL;
455 }
456 peer->server = link;
457 peer->ofd.fd = fd;
458 peer->ofd.data = peer;
459 peer->ofd.cb = ipa_server_peer_cb;
460 peer->ofd.when = BSC_FD_READ;
461 peer->cb = cb;
462 peer->data = data;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200463 INIT_LLIST_HEAD(&peer->tx_queue);
464
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200465 if (osmo_fd_register(&peer->ofd) < 0) {
466 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
467 talloc_free(peer);
468 return NULL;
469 }
470 return peer;
471}
472
473void ipa_server_peer_destroy(struct ipa_server_peer *peer)
474{
475 close(peer->ofd.fd);
476 osmo_fd_unregister(&peer->ofd);
477 talloc_free(peer);
478}
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200479
480void ipa_server_peer_send(struct ipa_server_peer *peer, struct msgb *msg)
481{
482 msgb_enqueue(&peer->tx_queue, msg);
483 peer->ofd.when |= BSC_FD_WRITE;
484}