blob: b4dbcb0b7cdf712d9eb81349b5edba0b94d9a27b [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>
Harald Welte71d87b22011-07-18 14:49:56 +020018#include <osmocom/core/talloc.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020019#include <osmocom/abis/e1_input.h>
20#include <osmocom/abis/ipaccess.h>
21#include <osmocom/core/socket.h>
Pablo Neira Ayusoef132692013-07-08 01:17:27 +020022#include <osmocom/core/backtrace.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020023
24#include <osmocom/abis/ipa.h>
25
Maxf4463672018-02-13 15:17:27 +010026#define LOGIPA(link, level, fmt, args...) LOGP(DLINP, level, "%s:%u " fmt, link->addr, link->port, ## args)
27
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +020028void ipa_msg_push_header(struct msgb *msg, uint8_t proto)
29{
30 struct ipaccess_head *hh;
31
32 msg->l2h = msg->data;
33 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
34 hh->proto = proto;
35 hh->len = htons(msgb_l2len(msg));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020036}
37
Pablo Neira Ayusof0995672011-09-08 12:58:38 +020038void ipa_client_conn_close(struct ipa_client_conn *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020039{
Harald Weltee68055b2013-06-30 14:21:54 +020040 /* be safe against multiple calls */
41 if (link->ofd->fd != -1) {
42 osmo_fd_unregister(link->ofd);
43 close(link->ofd->fd);
44 link->ofd->fd = -1;
45 }
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020046 msgb_free(link->pending_msg);
47 link->pending_msg = NULL;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020048}
49
Pau Espin Pedrol27757982018-08-28 17:32:29 +020050static int ipa_client_read(struct ipa_client_conn *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020051{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020052 struct osmo_fd *ofd = link->ofd;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020053 struct msgb *msg;
54 int ret;
55
Maxf4463672018-02-13 15:17:27 +010056 LOGIPA(link, LOGL_DEBUG, "message received\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020057
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020058 ret = ipa_msg_recv_buffered(ofd->fd, &msg, &link->pending_msg);
Pau Espin Pedrol493c8e62018-08-28 16:46:39 +020059 if (ret <= 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020060 if (ret == -EAGAIN)
Pau Espin Pedrol27757982018-08-28 17:32:29 +020061 return 0;
Pau Espin Pedrol493c8e62018-08-28 16:46:39 +020062 else if (ret == -EPIPE || ret == -ECONNRESET)
Maxf4463672018-02-13 15:17:27 +010063 LOGIPA(link, LOGL_ERROR, "lost connection with server\n");
Pau Espin Pedrol493c8e62018-08-28 16:46:39 +020064 else if (ret == 0)
65 LOGIPA(link, LOGL_ERROR, "connection closed with server\n");
Harald Welte51de9ca2013-06-30 20:13:16 +020066 ipa_client_conn_close(link);
67 if (link->updown_cb)
68 link->updown_cb(link, 0);
Pau Espin Pedrol27757982018-08-28 17:32:29 +020069 return -EBADF;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020070 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020071 if (link->read_cb)
Pau Espin Pedrol27757982018-08-28 17:32:29 +020072 return link->read_cb(link, msg);
73 return 0;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020074}
75
Pablo Neira Ayusof0995672011-09-08 12:58:38 +020076static void ipa_client_write(struct ipa_client_conn *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020077{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020078 if (link->write_cb)
79 link->write_cb(link);
80}
81
Pablo Neira Ayuso8ad30c92011-09-08 13:29:06 +020082static int ipa_client_write_default_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020083{
84 struct osmo_fd *ofd = link->ofd;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020085 struct msgb *msg;
86 struct llist_head *lh;
87 int ret;
88
Maxf4463672018-02-13 15:17:27 +010089 LOGIPA(link, LOGL_DEBUG, "sending data\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020090
91 if (llist_empty(&link->tx_queue)) {
92 ofd->when &= ~BSC_FD_WRITE;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020093 return 0;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020094 }
95 lh = link->tx_queue.next;
96 llist_del(lh);
97 msg = llist_entry(lh, struct msgb, list);
98
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020099 ret = send(link->ofd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200100 if (ret < 0) {
101 if (errno == EPIPE || errno == ENOTCONN) {
Harald Welte51de9ca2013-06-30 20:13:16 +0200102 ipa_client_conn_close(link);
103 if (link->updown_cb)
104 link->updown_cb(link, 0);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200105 }
Maxf4463672018-02-13 15:17:27 +0100106 LOGIPA(link, LOGL_ERROR, "error to send\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200107 }
108 msgb_free(msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200109 return 0;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200110}
111
Harald Welte7b6fc2e2011-08-19 21:58:48 +0200112static int ipa_client_fd_cb(struct osmo_fd *ofd, unsigned int what)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200113{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200114 struct ipa_client_conn *link = ofd->data;
Pau Espin Pedrol27757982018-08-28 17:32:29 +0200115 int error, ret = 0;
Harald Welte4301c372011-08-19 13:33:16 +0200116 socklen_t len = sizeof(error);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200117
118 switch(link->state) {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200119 case IPA_CLIENT_LINK_STATE_CONNECTING:
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200120 ret = getsockopt(ofd->fd, SOL_SOCKET, SO_ERROR, &error, &len);
121 if (ret >= 0 && error > 0) {
Harald Welte51de9ca2013-06-30 20:13:16 +0200122 ipa_client_conn_close(link);
123 if (link->updown_cb)
124 link->updown_cb(link, 0);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200125 return 0;
126 }
127 ofd->when &= ~BSC_FD_WRITE;
Maxf4463672018-02-13 15:17:27 +0100128 LOGIPA(link, LOGL_NOTICE, "connection done\n");
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200129 link->state = IPA_CLIENT_LINK_STATE_CONNECTED;
Harald Welte51de9ca2013-06-30 20:13:16 +0200130 if (link->updown_cb)
131 link->updown_cb(link, 1);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200132 break;
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200133 case IPA_CLIENT_LINK_STATE_CONNECTED:
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200134 if (what & BSC_FD_READ) {
Maxf4463672018-02-13 15:17:27 +0100135 LOGIPA(link, LOGL_DEBUG, "connected read\n");
Pau Espin Pedrol27757982018-08-28 17:32:29 +0200136 ret = ipa_client_read(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200137 }
Pau Espin Pedrol27757982018-08-28 17:32:29 +0200138 if (ret != -EBADF && (what & BSC_FD_WRITE)) {
Maxf4463672018-02-13 15:17:27 +0100139 LOGIPA(link, LOGL_DEBUG, "connected write\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200140 ipa_client_write(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200141 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200142 break;
143 default:
144 break;
145 }
146 return 0;
147}
148
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200149struct ipa_client_conn *
150ipa_client_conn_create(void *ctx, struct e1inp_ts *ts,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200151 int priv_nr, const char *addr, uint16_t port,
Harald Welte51de9ca2013-06-30 20:13:16 +0200152 void (*updown_cb)(struct ipa_client_conn *link, int up),
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200153 int (*read_cb)(struct ipa_client_conn *link,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200154 struct msgb *msgb),
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200155 int (*write_cb)(struct ipa_client_conn *link),
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200156 void *data)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200157{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200158 struct ipa_client_conn *ipa_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200159
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200160 ipa_link = talloc_zero(ctx, struct ipa_client_conn);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200161 if (!ipa_link)
162 return NULL;
163
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200164 if (ts) {
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200165 if (ts->line->driver == NULL) {
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200166 talloc_free(ipa_link);
167 return NULL;
168 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200169 ipa_link->ofd = &ts->driver.ipaccess.fd;
170 } else {
171 ipa_link->ofd = talloc_zero(ctx, struct osmo_fd);
172 if (ipa_link->ofd == NULL) {
173 talloc_free(ipa_link);
174 return NULL;
175 }
176 }
177
178 ipa_link->ofd->when |= BSC_FD_READ | BSC_FD_WRITE;
179 ipa_link->ofd->priv_nr = priv_nr;
180 ipa_link->ofd->cb = ipa_client_fd_cb;
181 ipa_link->ofd->data = ipa_link;
Harald Welte51de9ca2013-06-30 20:13:16 +0200182 ipa_link->ofd->fd = -1;
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200183 ipa_link->state = IPA_CLIENT_LINK_STATE_CONNECTING;
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200184 ipa_link->addr = talloc_strdup(ipa_link, addr);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200185 ipa_link->port = port;
Harald Welte51de9ca2013-06-30 20:13:16 +0200186 ipa_link->updown_cb = updown_cb;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200187 ipa_link->read_cb = read_cb;
Pablo Neira Ayuso8ad30c92011-09-08 13:29:06 +0200188 /* default to generic write callback if not set. */
189 if (write_cb == NULL)
190 ipa_link->write_cb = ipa_client_write_default_cb;
Pablo Neira Ayuso81ed7592012-08-22 16:35:17 +0200191 else
192 ipa_link->write_cb = write_cb;
193
Pablo Neira Ayuso2220a052011-09-08 18:43:31 +0200194 if (ts)
195 ipa_link->line = ts->line;
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200196 ipa_link->data = data;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200197 INIT_LLIST_HEAD(&ipa_link->tx_queue);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200198
199 return ipa_link;
200}
201
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200202void ipa_client_conn_destroy(struct ipa_client_conn *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200203{
204 talloc_free(link);
205}
206
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200207int ipa_client_conn_open(struct ipa_client_conn *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200208{
209 int ret;
210
Harald Weltee7e1b752014-08-17 21:41:34 +0200211 link->state = IPA_CLIENT_LINK_STATE_CONNECTING;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200212 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200213 link->addr, link->port,
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200214 OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK);
Harald Welte14dd30a2016-11-26 09:37:09 +0100215 if (ret < 0)
216 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200217 link->ofd->fd = ret;
Holger Hans Peter Freyther687046b2014-12-12 17:39:58 +0100218 link->ofd->when |= BSC_FD_WRITE;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200219 if (osmo_fd_register(link->ofd) < 0) {
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200220 close(ret);
Harald Welte51de9ca2013-06-30 20:13:16 +0200221 link->ofd->fd = -1;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200222 return -EIO;
223 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200224
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200225 return 0;
226}
227
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200228void ipa_client_conn_send(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200229{
230 msgb_enqueue(&link->tx_queue, msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200231 link->ofd->when |= BSC_FD_WRITE;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200232}
233
Holger Hans Peter Freyther90878592015-01-18 17:50:05 +0100234size_t ipa_client_conn_clear_queue(struct ipa_client_conn *link)
235{
236 size_t deleted = 0;
237
238 while (!llist_empty(&link->tx_queue)) {
239 struct msgb *msg = msgb_dequeue(&link->tx_queue);
240 msgb_free(msg);
241 deleted += 1;
242 }
243
244 link->ofd->when &= ~BSC_FD_WRITE;
245 return deleted;
246}
247
Harald Welte7b6fc2e2011-08-19 21:58:48 +0200248static int ipa_server_fd_cb(struct osmo_fd *ofd, unsigned int what)
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200249{
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200250 int fd, ret;
Max480073a2019-01-20 12:43:45 +0100251 char ipbuf[INET6_ADDRSTRLEN + 1];
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200252 struct sockaddr_in sa;
253 socklen_t sa_len = sizeof(sa);
254 struct ipa_server_link *link = ofd->data;
255
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200256 fd = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len);
257 if (fd < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200258 LOGP(DLINP, LOGL_ERROR, "failed to accept from origin "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200259 "peer, reason=`%s'\n", strerror(errno));
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200260 return fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200261 }
Max480073a2019-01-20 12:43:45 +0100262
263 if (!link->addr) {
264 ret = osmo_sock_get_local_ip(fd, ipbuf, INET6_ADDRSTRLEN + 1);
265 if (ret == 0)
266 link->addr = talloc_strdup(link, ipbuf);
267 }
268
Max3a2aa092019-01-20 12:48:47 +0100269 LOGIPA(link, LOGL_NOTICE, "accept()ed new link from %s:%u\n",
270 inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200271
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200272 ret = link->accept_cb(link, fd);
273 if (ret < 0) {
274 LOGP(DLINP, LOGL_ERROR,
Debian Mobcom Maintainers418775c2018-08-09 21:32:08 +0200275 "failed to process accept()ed new link, "
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200276 "reason=`%s'\n", strerror(-ret));
277 close(fd);
278 return ret;
279 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200280
281 return 0;
282}
283
284struct ipa_server_link *
285ipa_server_link_create(void *ctx, struct e1inp_line *line,
286 const char *addr, uint16_t port,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200287 int (*accept_cb)(struct ipa_server_link *link, int fd),
288 void *data)
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200289{
290 struct ipa_server_link *ipa_link;
291
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200292 OSMO_ASSERT(accept_cb != NULL);
293
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200294 ipa_link = talloc_zero(ctx, struct ipa_server_link);
295 if (!ipa_link)
296 return NULL;
297
298 ipa_link->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
299 ipa_link->ofd.cb = ipa_server_fd_cb;
Alexander Couzense11afda2019-08-28 02:35:10 +0200300 ipa_link->ofd.fd = -1;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200301 ipa_link->ofd.data = ipa_link;
Max480073a2019-01-20 12:43:45 +0100302 if (addr)
303 ipa_link->addr = talloc_strdup(ipa_link, addr);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200304 ipa_link->port = port;
305 ipa_link->accept_cb = accept_cb;
306 ipa_link->line = line;
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200307 ipa_link->data = data;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200308
309 return ipa_link;
310
311}
312
313void ipa_server_link_destroy(struct ipa_server_link *link)
314{
315 talloc_free(link);
316}
317
318int ipa_server_link_open(struct ipa_server_link *link)
319{
320 int ret;
321
322 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
323 link->addr, link->port, OSMO_SOCK_F_BIND);
324 if (ret < 0)
325 return ret;
326
327 link->ofd.fd = ret;
328 if (osmo_fd_register(&link->ofd) < 0) {
329 close(ret);
Alexander Couzense11afda2019-08-28 02:35:10 +0200330 link->ofd.fd = -1;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200331 return -EIO;
332 }
333 return 0;
334}
335
336void ipa_server_link_close(struct ipa_server_link *link)
337{
Alexander Couzense11afda2019-08-28 02:35:10 +0200338 if (link->ofd.fd == -1)
339 return;
340
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200341 osmo_fd_unregister(&link->ofd);
342 close(link->ofd.fd);
Alexander Couzense11afda2019-08-28 02:35:10 +0200343 link->ofd.fd = -1;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200344}
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200345
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200346static int ipa_server_conn_read(struct ipa_server_conn *conn)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200347{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200348 struct osmo_fd *ofd = &conn->ofd;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200349 struct msgb *msg;
350 int ret;
351
Maxf4463672018-02-13 15:17:27 +0100352 LOGIPA(conn, LOGL_DEBUG, "message received\n");
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200353
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200354 ret = ipa_msg_recv_buffered(ofd->fd, &msg, &conn->pending_msg);
Pau Espin Pedrol3750dea2018-08-28 17:49:29 +0200355 if (ret <= 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200356 if (ret == -EAGAIN)
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200357 return 0;
Pau Espin Pedrol3750dea2018-08-28 17:49:29 +0200358 else if (ret == -EPIPE || ret == -ECONNRESET)
Maxf4463672018-02-13 15:17:27 +0100359 LOGIPA(conn, LOGL_ERROR, "lost connection with server\n");
Pau Espin Pedrol3750dea2018-08-28 17:49:29 +0200360 else if (ret == 0)
361 LOGIPA(conn, LOGL_ERROR, "connection closed with server\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200362 ipa_server_conn_destroy(conn);
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200363 return -EBADF;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200364 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200365 if (conn->cb)
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200366 return conn->cb(conn, msg);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200367
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200368 return 0;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200369}
370
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200371static void ipa_server_conn_write(struct ipa_server_conn *conn)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200372{
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200373 struct msgb *msg;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200374 int ret;
375
Maxf4463672018-02-13 15:17:27 +0100376 LOGIPA(conn, LOGL_DEBUG, "sending data\n");
Neels Hofmeyr01543a12017-09-14 04:32:19 +0200377 msg = msgb_dequeue(&conn->tx_queue);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200378
Neels Hofmeyr01543a12017-09-14 04:32:19 +0200379 if (!msg) {
380 conn->ofd.when &= ~BSC_FD_WRITE;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200381 return;
382 }
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200383
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200384 ret = send(conn->ofd.fd, msg->data, msg->len, 0);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200385 if (ret < 0) {
Maxf4463672018-02-13 15:17:27 +0100386 LOGIPA(conn, LOGL_ERROR, "error to send\n");
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200387 }
388 msgb_free(msg);
389}
390
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200391static int ipa_server_conn_cb(struct osmo_fd *ofd, unsigned int what)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200392{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200393 struct ipa_server_conn *conn = ofd->data;
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200394 int rc = 0;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200395
Pablo Neira Ayuso218bb8f2011-09-09 01:04:46 +0200396 LOGP(DLINP, LOGL_DEBUG, "connected read/write\n");
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200397 if (what & BSC_FD_READ)
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200398 rc = ipa_server_conn_read(conn);
399 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200400 ipa_server_conn_write(conn);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200401
402 return 0;
403}
404
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200405struct ipa_server_conn *
406ipa_server_conn_create(void *ctx, struct ipa_server_link *link, int fd,
407 int (*cb)(struct ipa_server_conn *conn, struct msgb *msg),
Daniel Willmanna0d93312011-09-15 12:56:58 +0200408 int (*closed_cb)(struct ipa_server_conn *conn), void *data)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200409{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200410 struct ipa_server_conn *conn;
Harald Welteb2d727a2016-04-28 07:53:49 +0200411 struct sockaddr_in sa;
412 socklen_t sa_len = sizeof(sa);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200413
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200414 conn = talloc_zero(ctx, struct ipa_server_conn);
415 if (conn == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200416 LOGP(DLINP, LOGL_ERROR, "cannot allocate new peer in server, "
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200417 "reason=`%s'\n", strerror(errno));
418 return NULL;
419 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200420 conn->server = link;
421 conn->ofd.fd = fd;
422 conn->ofd.data = conn;
423 conn->ofd.cb = ipa_server_conn_cb;
424 conn->ofd.when = BSC_FD_READ;
425 conn->cb = cb;
Daniel Willmanna0d93312011-09-15 12:56:58 +0200426 conn->closed_cb = closed_cb;
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200427 conn->data = data;
428 INIT_LLIST_HEAD(&conn->tx_queue);
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200429
Harald Welteb2d727a2016-04-28 07:53:49 +0200430 if (!getpeername(fd, (struct sockaddr *)&sa, &sa_len)) {
431 char *str = inet_ntoa(sa.sin_addr);
432 conn->addr = talloc_strdup(conn, str);
433 conn->port = ntohs(sa.sin_port);
434 }
435
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200436 if (osmo_fd_register(&conn->ofd) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200437 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200438 talloc_free(conn);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200439 return NULL;
440 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200441 return conn;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200442}
443
Harald Welte12814b92016-04-28 07:54:31 +0200444int ipa_server_conn_ccm(struct ipa_server_conn *conn, struct msgb *msg)
445{
446 struct tlv_parsed tlvp;
447 uint8_t msg_type = *(msg->l2h);
448 struct ipaccess_unit unit_data = {};
449 char *unitid;
450 int len, rc;
451
452 /* shared CCM handling on both server and client */
453 rc = ipa_ccm_rcvmsg_base(msg, &conn->ofd);
454 switch (rc) {
455 case -1:
456 /* error in IPA CCM processing */
457 goto err;
458 case 1:
459 /* IPA CCM message that was handled in _base */
460 return 0;
461 case 0:
462 /* IPA CCM message that we need to handle */
463 break;
464 default:
465 /* Error */
Maxf4463672018-02-13 15:17:27 +0100466 LOGIPA(conn, LOGL_ERROR, "Unexpected return from "
Harald Welte12814b92016-04-28 07:54:31 +0200467 "ipa_ccm_rcvmsg_base: %d\n", rc);
468 goto err;
469 }
470
471 switch (msg_type) {
472 case IPAC_MSGT_ID_RESP:
Harald Welte82eb55e2018-08-01 13:22:55 +0200473 rc = ipa_ccm_id_resp_parse(&tlvp, (const uint8_t *)msg->l2h+1, msgb_l2len(msg)-1);
Harald Welte12814b92016-04-28 07:54:31 +0200474 if (rc < 0) {
Maxf4463672018-02-13 15:17:27 +0100475 LOGIPA(conn, LOGL_ERROR, "IPA CCM RESPonse with "
Harald Welte12814b92016-04-28 07:54:31 +0200476 "malformed TLVs\n");
477 goto err;
478 }
479 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Maxf4463672018-02-13 15:17:27 +0100480 LOGIPA(conn, LOGL_ERROR, "IPA CCM RESP without "
Harald Welte12814b92016-04-28 07:54:31 +0200481 "unit ID\n");
482 goto err;
483 }
484 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
485 if (len < 1) {
Maxf4463672018-02-13 15:17:27 +0100486 LOGIPA(conn, LOGL_ERROR, "IPA CCM RESP with short"
Harald Welte12814b92016-04-28 07:54:31 +0200487 "unit ID\n");
488 goto err;
489 }
490 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
491 unitid[len-1] = '\0';
492 ipa_parse_unitid(unitid, &unit_data);
493
494 /* FIXME */
495 rc = conn->ccm_cb(conn, msg, &tlvp, &unit_data);
496 if (rc < 0)
497 goto err;
498 break;
499 default:
Maxf4463672018-02-13 15:17:27 +0100500 LOGIPA(conn, LOGL_ERROR, "Unknown IPA message type\n");
Harald Welte12814b92016-04-28 07:54:31 +0200501 break;
502 }
503 return 0;
504err:
505 /* in case of any error, we close the connection */
506 ipa_server_conn_destroy(conn);
507 return -1;
508}
509
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200510void ipa_server_conn_destroy(struct ipa_server_conn *conn)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200511{
Harald Weltec0a0ec42019-03-04 22:32:52 +0100512 /* make the function re-entrant in case closed_cb() below somehow
513 * calls again into this destructor */
514 if (conn->ofd.fd == -1)
515 return;
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200516 close(conn->ofd.fd);
Harald Weltec0a0ec42019-03-04 22:32:52 +0100517 conn->ofd.fd = -1;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200518 msgb_free(conn->pending_msg);
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200519 osmo_fd_unregister(&conn->ofd);
Daniel Willmanna0d93312011-09-15 12:56:58 +0200520 if (conn->closed_cb)
521 conn->closed_cb(conn);
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200522 talloc_free(conn);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200523}
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200524
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200525void ipa_server_conn_send(struct ipa_server_conn *conn, struct msgb *msg)
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200526{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200527 msgb_enqueue(&conn->tx_queue, msg);
528 conn->ofd.when |= BSC_FD_WRITE;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200529}