blob: 0f67dcaee7723087af94fdec5efe61bba8924871 [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{
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100158 return ipa_client_conn_create2(ctx, ts, priv_nr, NULL, 0, addr, port,
159 updown_cb, read_cb, write_cb, data);
160}
161
162struct ipa_client_conn *
163ipa_client_conn_create2(void *ctx, struct e1inp_ts *ts,
164 int priv_nr, const char *loc_addr, uint16_t loc_port,
165 const char *rem_addr, uint16_t rem_port,
166 void (*updown_cb)(struct ipa_client_conn *link, int up),
167 int (*read_cb)(struct ipa_client_conn *link,
168 struct msgb *msgb),
169 int (*write_cb)(struct ipa_client_conn *link),
170 void *data)
171{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200172 struct ipa_client_conn *ipa_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200173
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200174 ipa_link = talloc_zero(ctx, struct ipa_client_conn);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200175 if (!ipa_link)
176 return NULL;
177
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200178 if (ts) {
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200179 if (ts->line->driver == NULL) {
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200180 talloc_free(ipa_link);
181 return NULL;
182 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200183 ipa_link->ofd = &ts->driver.ipaccess.fd;
184 } else {
185 ipa_link->ofd = talloc_zero(ctx, struct osmo_fd);
186 if (ipa_link->ofd == NULL) {
187 talloc_free(ipa_link);
188 return NULL;
189 }
190 }
191
192 ipa_link->ofd->when |= BSC_FD_READ | BSC_FD_WRITE;
193 ipa_link->ofd->priv_nr = priv_nr;
194 ipa_link->ofd->cb = ipa_client_fd_cb;
195 ipa_link->ofd->data = ipa_link;
Harald Welte51de9ca2013-06-30 20:13:16 +0200196 ipa_link->ofd->fd = -1;
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200197 ipa_link->state = IPA_CLIENT_LINK_STATE_CONNECTING;
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100198 ipa_link->local_addr = talloc_strdup(ipa_link, loc_addr);
199 ipa_link->local_port = loc_port;
200 ipa_link->addr = talloc_strdup(ipa_link, rem_addr);
201 ipa_link->port = rem_port;
Harald Welte51de9ca2013-06-30 20:13:16 +0200202 ipa_link->updown_cb = updown_cb;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200203 ipa_link->read_cb = read_cb;
Pablo Neira Ayuso8ad30c92011-09-08 13:29:06 +0200204 /* default to generic write callback if not set. */
205 if (write_cb == NULL)
206 ipa_link->write_cb = ipa_client_write_default_cb;
Pablo Neira Ayuso81ed7592012-08-22 16:35:17 +0200207 else
208 ipa_link->write_cb = write_cb;
209
Pablo Neira Ayuso2220a052011-09-08 18:43:31 +0200210 if (ts)
211 ipa_link->line = ts->line;
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200212 ipa_link->data = data;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200213 INIT_LLIST_HEAD(&ipa_link->tx_queue);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200214
215 return ipa_link;
216}
217
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200218void ipa_client_conn_destroy(struct ipa_client_conn *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200219{
220 talloc_free(link);
221}
222
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200223int ipa_client_conn_open(struct ipa_client_conn *link)
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200224{
225 int ret;
226
Harald Weltee7e1b752014-08-17 21:41:34 +0200227 link->state = IPA_CLIENT_LINK_STATE_CONNECTING;
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100228 ret = osmo_sock_init2(AF_INET, SOCK_STREAM, IPPROTO_TCP,
229 link->local_addr, link->local_port,
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200230 link->addr, link->port,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100231 OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK);
Harald Welte14dd30a2016-11-26 09:37:09 +0100232 if (ret < 0)
233 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200234 link->ofd->fd = ret;
Holger Hans Peter Freyther687046b2014-12-12 17:39:58 +0100235 link->ofd->when |= BSC_FD_WRITE;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200236 if (osmo_fd_register(link->ofd) < 0) {
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200237 close(ret);
Harald Welte51de9ca2013-06-30 20:13:16 +0200238 link->ofd->fd = -1;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200239 return -EIO;
240 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200241
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200242 return 0;
243}
244
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200245void ipa_client_conn_send(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200246{
247 msgb_enqueue(&link->tx_queue, msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200248 link->ofd->when |= BSC_FD_WRITE;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200249}
250
Holger Hans Peter Freyther90878592015-01-18 17:50:05 +0100251size_t ipa_client_conn_clear_queue(struct ipa_client_conn *link)
252{
253 size_t deleted = 0;
254
255 while (!llist_empty(&link->tx_queue)) {
256 struct msgb *msg = msgb_dequeue(&link->tx_queue);
257 msgb_free(msg);
258 deleted += 1;
259 }
260
261 link->ofd->when &= ~BSC_FD_WRITE;
262 return deleted;
263}
264
Harald Welte7b6fc2e2011-08-19 21:58:48 +0200265static int ipa_server_fd_cb(struct osmo_fd *ofd, unsigned int what)
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200266{
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200267 int fd, ret;
Max480073a2019-01-20 12:43:45 +0100268 char ipbuf[INET6_ADDRSTRLEN + 1];
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200269 struct sockaddr_in sa;
270 socklen_t sa_len = sizeof(sa);
271 struct ipa_server_link *link = ofd->data;
272
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200273 fd = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len);
274 if (fd < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200275 LOGP(DLINP, LOGL_ERROR, "failed to accept from origin "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200276 "peer, reason=`%s'\n", strerror(errno));
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200277 return fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200278 }
Max480073a2019-01-20 12:43:45 +0100279
280 if (!link->addr) {
281 ret = osmo_sock_get_local_ip(fd, ipbuf, INET6_ADDRSTRLEN + 1);
282 if (ret == 0)
283 link->addr = talloc_strdup(link, ipbuf);
284 }
285
Max3a2aa092019-01-20 12:48:47 +0100286 LOGIPA(link, LOGL_NOTICE, "accept()ed new link from %s:%u\n",
287 inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200288
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200289 ret = link->accept_cb(link, fd);
290 if (ret < 0) {
291 LOGP(DLINP, LOGL_ERROR,
Debian Mobcom Maintainers418775c2018-08-09 21:32:08 +0200292 "failed to process accept()ed new link, "
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200293 "reason=`%s'\n", strerror(-ret));
294 close(fd);
295 return ret;
296 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200297
298 return 0;
299}
300
301struct ipa_server_link *
302ipa_server_link_create(void *ctx, struct e1inp_line *line,
303 const char *addr, uint16_t port,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200304 int (*accept_cb)(struct ipa_server_link *link, int fd),
305 void *data)
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200306{
307 struct ipa_server_link *ipa_link;
308
Pablo Neira Ayusocdda0a82013-07-08 01:13:19 +0200309 OSMO_ASSERT(accept_cb != NULL);
310
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200311 ipa_link = talloc_zero(ctx, struct ipa_server_link);
312 if (!ipa_link)
313 return NULL;
314
315 ipa_link->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
316 ipa_link->ofd.cb = ipa_server_fd_cb;
Alexander Couzense11afda2019-08-28 02:35:10 +0200317 ipa_link->ofd.fd = -1;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200318 ipa_link->ofd.data = ipa_link;
Max480073a2019-01-20 12:43:45 +0100319 if (addr)
320 ipa_link->addr = talloc_strdup(ipa_link, addr);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200321 ipa_link->port = port;
322 ipa_link->accept_cb = accept_cb;
323 ipa_link->line = line;
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200324 ipa_link->data = data;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200325
326 return ipa_link;
327
328}
329
330void ipa_server_link_destroy(struct ipa_server_link *link)
331{
332 talloc_free(link);
333}
334
335int ipa_server_link_open(struct ipa_server_link *link)
336{
337 int ret;
338
339 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
340 link->addr, link->port, OSMO_SOCK_F_BIND);
341 if (ret < 0)
342 return ret;
343
344 link->ofd.fd = ret;
345 if (osmo_fd_register(&link->ofd) < 0) {
346 close(ret);
Alexander Couzense11afda2019-08-28 02:35:10 +0200347 link->ofd.fd = -1;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200348 return -EIO;
349 }
350 return 0;
351}
352
353void ipa_server_link_close(struct ipa_server_link *link)
354{
Alexander Couzense11afda2019-08-28 02:35:10 +0200355 if (link->ofd.fd == -1)
356 return;
357
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200358 osmo_fd_unregister(&link->ofd);
359 close(link->ofd.fd);
Alexander Couzense11afda2019-08-28 02:35:10 +0200360 link->ofd.fd = -1;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200361}
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200362
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200363static int ipa_server_conn_read(struct ipa_server_conn *conn)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200364{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200365 struct osmo_fd *ofd = &conn->ofd;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200366 struct msgb *msg;
367 int ret;
368
Maxf4463672018-02-13 15:17:27 +0100369 LOGIPA(conn, LOGL_DEBUG, "message received\n");
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200370
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200371 ret = ipa_msg_recv_buffered(ofd->fd, &msg, &conn->pending_msg);
Pau Espin Pedrol3750dea2018-08-28 17:49:29 +0200372 if (ret <= 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200373 if (ret == -EAGAIN)
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200374 return 0;
Pau Espin Pedrol3750dea2018-08-28 17:49:29 +0200375 else if (ret == -EPIPE || ret == -ECONNRESET)
Maxf4463672018-02-13 15:17:27 +0100376 LOGIPA(conn, LOGL_ERROR, "lost connection with server\n");
Pau Espin Pedrol3750dea2018-08-28 17:49:29 +0200377 else if (ret == 0)
378 LOGIPA(conn, LOGL_ERROR, "connection closed with server\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200379 ipa_server_conn_destroy(conn);
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200380 return -EBADF;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200381 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200382 if (conn->cb)
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200383 return conn->cb(conn, msg);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200384
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200385 return 0;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200386}
387
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200388static void ipa_server_conn_write(struct ipa_server_conn *conn)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200389{
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200390 struct msgb *msg;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200391 int ret;
392
Maxf4463672018-02-13 15:17:27 +0100393 LOGIPA(conn, LOGL_DEBUG, "sending data\n");
Neels Hofmeyr01543a12017-09-14 04:32:19 +0200394 msg = msgb_dequeue(&conn->tx_queue);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200395
Neels Hofmeyr01543a12017-09-14 04:32:19 +0200396 if (!msg) {
397 conn->ofd.when &= ~BSC_FD_WRITE;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200398 return;
399 }
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200400
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200401 ret = send(conn->ofd.fd, msg->data, msg->len, 0);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200402 if (ret < 0) {
Maxf4463672018-02-13 15:17:27 +0100403 LOGIPA(conn, LOGL_ERROR, "error to send\n");
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200404 }
405 msgb_free(msg);
406}
407
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200408static int ipa_server_conn_cb(struct osmo_fd *ofd, unsigned int what)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200409{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200410 struct ipa_server_conn *conn = ofd->data;
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200411 int rc = 0;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200412
Pablo Neira Ayuso218bb8f2011-09-09 01:04:46 +0200413 LOGP(DLINP, LOGL_DEBUG, "connected read/write\n");
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200414 if (what & BSC_FD_READ)
Pau Espin Pedrol082876b2018-08-28 17:53:38 +0200415 rc = ipa_server_conn_read(conn);
416 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200417 ipa_server_conn_write(conn);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200418
419 return 0;
420}
421
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200422struct ipa_server_conn *
423ipa_server_conn_create(void *ctx, struct ipa_server_link *link, int fd,
424 int (*cb)(struct ipa_server_conn *conn, struct msgb *msg),
Daniel Willmanna0d93312011-09-15 12:56:58 +0200425 int (*closed_cb)(struct ipa_server_conn *conn), void *data)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200426{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200427 struct ipa_server_conn *conn;
Harald Welteb2d727a2016-04-28 07:53:49 +0200428 struct sockaddr_in sa;
429 socklen_t sa_len = sizeof(sa);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200430
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200431 conn = talloc_zero(ctx, struct ipa_server_conn);
432 if (conn == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200433 LOGP(DLINP, LOGL_ERROR, "cannot allocate new peer in server, "
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200434 "reason=`%s'\n", strerror(errno));
435 return NULL;
436 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200437 conn->server = link;
438 conn->ofd.fd = fd;
439 conn->ofd.data = conn;
440 conn->ofd.cb = ipa_server_conn_cb;
441 conn->ofd.when = BSC_FD_READ;
442 conn->cb = cb;
Daniel Willmanna0d93312011-09-15 12:56:58 +0200443 conn->closed_cb = closed_cb;
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200444 conn->data = data;
445 INIT_LLIST_HEAD(&conn->tx_queue);
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200446
Harald Welteb2d727a2016-04-28 07:53:49 +0200447 if (!getpeername(fd, (struct sockaddr *)&sa, &sa_len)) {
448 char *str = inet_ntoa(sa.sin_addr);
449 conn->addr = talloc_strdup(conn, str);
450 conn->port = ntohs(sa.sin_port);
451 }
452
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200453 if (osmo_fd_register(&conn->ofd) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200454 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200455 talloc_free(conn);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200456 return NULL;
457 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200458 return conn;
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200459}
460
Harald Welte12814b92016-04-28 07:54:31 +0200461int ipa_server_conn_ccm(struct ipa_server_conn *conn, struct msgb *msg)
462{
463 struct tlv_parsed tlvp;
464 uint8_t msg_type = *(msg->l2h);
465 struct ipaccess_unit unit_data = {};
466 char *unitid;
467 int len, rc;
468
469 /* shared CCM handling on both server and client */
470 rc = ipa_ccm_rcvmsg_base(msg, &conn->ofd);
471 switch (rc) {
472 case -1:
473 /* error in IPA CCM processing */
474 goto err;
475 case 1:
476 /* IPA CCM message that was handled in _base */
477 return 0;
478 case 0:
479 /* IPA CCM message that we need to handle */
480 break;
481 default:
482 /* Error */
Maxf4463672018-02-13 15:17:27 +0100483 LOGIPA(conn, LOGL_ERROR, "Unexpected return from "
Harald Welte12814b92016-04-28 07:54:31 +0200484 "ipa_ccm_rcvmsg_base: %d\n", rc);
485 goto err;
486 }
487
488 switch (msg_type) {
489 case IPAC_MSGT_ID_RESP:
Harald Welte82eb55e2018-08-01 13:22:55 +0200490 rc = ipa_ccm_id_resp_parse(&tlvp, (const uint8_t *)msg->l2h+1, msgb_l2len(msg)-1);
Harald Welte12814b92016-04-28 07:54:31 +0200491 if (rc < 0) {
Maxf4463672018-02-13 15:17:27 +0100492 LOGIPA(conn, LOGL_ERROR, "IPA CCM RESPonse with "
Harald Welte12814b92016-04-28 07:54:31 +0200493 "malformed TLVs\n");
494 goto err;
495 }
496 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Maxf4463672018-02-13 15:17:27 +0100497 LOGIPA(conn, LOGL_ERROR, "IPA CCM RESP without "
Harald Welte12814b92016-04-28 07:54:31 +0200498 "unit ID\n");
499 goto err;
500 }
501 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
502 if (len < 1) {
Maxf4463672018-02-13 15:17:27 +0100503 LOGIPA(conn, LOGL_ERROR, "IPA CCM RESP with short"
Harald Welte12814b92016-04-28 07:54:31 +0200504 "unit ID\n");
505 goto err;
506 }
507 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
508 unitid[len-1] = '\0';
509 ipa_parse_unitid(unitid, &unit_data);
510
511 /* FIXME */
512 rc = conn->ccm_cb(conn, msg, &tlvp, &unit_data);
513 if (rc < 0)
514 goto err;
515 break;
516 default:
Maxf4463672018-02-13 15:17:27 +0100517 LOGIPA(conn, LOGL_ERROR, "Unknown IPA message type\n");
Harald Welte12814b92016-04-28 07:54:31 +0200518 break;
519 }
520 return 0;
521err:
522 /* in case of any error, we close the connection */
523 ipa_server_conn_destroy(conn);
524 return -1;
525}
526
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200527void ipa_server_conn_destroy(struct ipa_server_conn *conn)
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200528{
Harald Weltec0a0ec42019-03-04 22:32:52 +0100529 /* make the function re-entrant in case closed_cb() below somehow
530 * calls again into this destructor */
531 if (conn->ofd.fd == -1)
532 return;
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200533 close(conn->ofd.fd);
Harald Weltec0a0ec42019-03-04 22:32:52 +0100534 conn->ofd.fd = -1;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200535 msgb_free(conn->pending_msg);
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200536 osmo_fd_unregister(&conn->ofd);
Daniel Willmanna0d93312011-09-15 12:56:58 +0200537 if (conn->closed_cb)
538 conn->closed_cb(conn);
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200539 talloc_free(conn);
Pablo Neira Ayuso6af9b612011-06-23 14:07:47 +0200540}
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200541
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200542void ipa_server_conn_send(struct ipa_server_conn *conn, struct msgb *msg)
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200543{
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200544 msgb_enqueue(&conn->tx_queue, msg);
545 conn->ofd.when |= BSC_FD_WRITE;
Pablo Neira Ayusoaf3fed92011-06-23 20:42:19 +0200546}