ubus: increase message size limit and make it configurable at build-time
[project/ubus.git] / libubus-io.c
1 /*
2  * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <sys/types.h>
15 #include <sys/uio.h>
16 #include <sys/socket.h>
17
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <poll.h>
21
22 #include <libubox/usock.h>
23 #include <libubox/blob.h>
24 #include <libubox/blobmsg.h>
25
26 #include "libubus.h"
27 #include "libubus-internal.h"
28
29 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
30
31 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
32         [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
33         [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
34         [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
35         [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
36         [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
37         [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
38         [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
39 };
40
41 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
42
43 __hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
44 {
45         blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
46         return attrbuf;
47 }
48
49 static void wait_data(int fd, bool write)
50 {
51         struct pollfd pfd = { .fd = fd };
52
53         pfd.events = write ? POLLOUT : POLLIN;
54         poll(&pfd, 1, 0);
55 }
56
57 static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
58 {
59         static struct {
60                 struct cmsghdr h;
61                 int fd;
62         } fd_buf = {
63                 .h = {
64                         .cmsg_len = sizeof(fd_buf),
65                         .cmsg_level = SOL_SOCKET,
66                         .cmsg_type = SCM_RIGHTS,
67                 }
68         };
69         struct msghdr msghdr = {
70                 .msg_iov = iov,
71                 .msg_iovlen = iov_len,
72                 .msg_control = &fd_buf,
73                 .msg_controllen = sizeof(fd_buf),
74         };
75         int len = 0;
76
77         do {
78                 int cur_len;
79
80                 if (sock_fd < 0) {
81                         msghdr.msg_control = NULL;
82                         msghdr.msg_controllen = 0;
83                 } else {
84                         fd_buf.fd = sock_fd;
85                 }
86
87                 cur_len = sendmsg(fd, &msghdr, 0);
88                 if (cur_len < 0) {
89                         switch(errno) {
90                         case EAGAIN:
91                                 wait_data(fd, true);
92                                 break;
93                         case EINTR:
94                                 break;
95                         default:
96                                 return -1;
97                         }
98                         continue;
99                 }
100
101                 if (len > 0)
102                         sock_fd = -1;
103
104                 len += cur_len;
105                 while (cur_len >= iov->iov_len) {
106                         cur_len -= iov->iov_len;
107                         iov_len--;
108                         iov++;
109                         if (!iov_len)
110                                 return len;
111                 }
112                 iov->iov_len -= cur_len;
113         } while (1);
114
115         /* Should never reach here */
116         return -1;
117 }
118
119 int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
120                            struct blob_attr *msg, int cmd, uint32_t peer, int fd)
121 {
122         struct ubus_msghdr hdr;
123         struct iovec iov[2] = {
124                 STATIC_IOV(hdr)
125         };
126         int ret;
127
128         hdr.version = 0;
129         hdr.type = cmd;
130         hdr.seq = seq;
131         hdr.peer = peer;
132
133         if (!msg) {
134                 blob_buf_init(&b, 0);
135                 msg = b.head;
136         }
137
138         iov[1].iov_base = (char *) msg;
139         iov[1].iov_len = blob_raw_len(msg);
140
141         ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov), fd);
142         if (ret < 0)
143                 ctx->sock.eof = true;
144
145         if (fd >= 0)
146                 close(fd);
147
148         return ret;
149 }
150
151 static int recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
152 {
153         int bytes, total = 0;
154         static struct {
155                 struct cmsghdr h;
156                 int fd;
157         } fd_buf = {
158                 .h = {
159                         .cmsg_type = SCM_RIGHTS,
160                         .cmsg_level = SOL_SOCKET,
161                         .cmsg_len = sizeof(fd_buf),
162                 },
163         };
164         struct msghdr msghdr = {
165                 .msg_iov = iov,
166                 .msg_iovlen = 1,
167         };
168
169         while (iov->iov_len > 0) {
170                 if (wait)
171                         wait_data(fd, false);
172
173                 if (recv_fd) {
174                         msghdr.msg_control = &fd_buf;
175                         msghdr.msg_controllen = sizeof(fd_buf);
176                 } else {
177                         msghdr.msg_control = NULL;
178                         msghdr.msg_controllen = 0;
179                 }
180
181                 fd_buf.fd = -1;
182                 bytes = recvmsg(fd, &msghdr, 0);
183                 if (!bytes)
184                         return -1;
185
186                 if (bytes < 0) {
187                         bytes = 0;
188                         if (uloop_cancelled)
189                                 return 0;
190                         if (errno == EINTR)
191                                 continue;
192
193                         if (errno != EAGAIN)
194                                 return -1;
195                 }
196                 if (!wait && !bytes)
197                         return 0;
198
199                 if (recv_fd)
200                         *recv_fd = fd_buf.fd;
201
202                 recv_fd = NULL;
203
204                 wait = true;
205                 iov->iov_len -= bytes;
206                 iov->iov_base += bytes;
207                 total += bytes;
208         }
209
210         return total;
211 }
212
213 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
214 {
215         struct blob_attr *data = (struct blob_attr *) (hdr + 1);
216
217         if (hdr->version != 0)
218                 return false;
219
220         if (blob_raw_len(data) < sizeof(*data))
221                 return false;
222
223         if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
224                 return false;
225
226         return true;
227 }
228
229 static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
230 {
231         struct {
232                 struct ubus_msghdr hdr;
233                 struct blob_attr data;
234         } hdrbuf;
235         struct iovec iov = STATIC_IOV(hdrbuf);
236         int len;
237         int r;
238
239         /* receive header + start attribute */
240         r = recv_retry(ctx->sock.fd, &iov, false, recv_fd);
241         if (r <= 0) {
242                 if (r < 0)
243                         ctx->sock.eof = true;
244
245                 return false;
246         }
247
248         if (!ubus_validate_hdr(&hdrbuf.hdr))
249                 return false;
250
251         len = blob_raw_len(&hdrbuf.data);
252         if (len > ctx->msgbuf_data_len) {
253                 ctx->msgbuf.data = realloc(ctx->msgbuf.data, len * sizeof(char));
254                 if (ctx->msgbuf.data)
255                         ctx->msgbuf_data_len = len;
256         }
257         if (!ctx->msgbuf.data)
258                 return false;
259
260         memcpy(&ctx->msgbuf.hdr, &hdrbuf.hdr, sizeof(hdrbuf.hdr));
261         memcpy(ctx->msgbuf.data, &hdrbuf.data, sizeof(hdrbuf.data));
262
263         iov.iov_base = (char *)ctx->msgbuf.data + sizeof(hdrbuf.data);
264         iov.iov_len = blob_len(ctx->msgbuf.data);
265         if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true, NULL))
266                 return false;
267
268         return true;
269 }
270
271 void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
272 {
273         struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
274         struct ubus_msghdr *hdr = &ctx->msgbuf.hdr;
275         int recv_fd = -1;
276
277         while (get_next_msg(ctx, &recv_fd)) {
278                 ubus_process_msg(ctx, hdr, recv_fd);
279                 if (uloop_cancelled)
280                         break;
281         }
282
283         if (u->eof)
284                 ctx->connection_lost(ctx);
285 }
286
287 void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
288 {
289         struct pollfd pfd = {
290                 .fd = ctx->sock.fd,
291                 .events = POLLIN | POLLERR,
292         };
293
294         poll(&pfd, 1, timeout);
295         ubus_handle_data(&ctx->sock, ULOOP_READ);
296 }
297
298 static void
299 ubus_refresh_state(struct ubus_context *ctx)
300 {
301         struct ubus_object *obj, *tmp;
302         struct ubus_object **objs;
303         int n, i = 0;
304
305         /* clear all type IDs, they need to be registered again */
306         avl_for_each_element(&ctx->objects, obj, avl)
307                 if (obj->type)
308                         obj->type->id = 0;
309
310         /* push out all objects again */
311         objs = alloca(ctx->objects.count * sizeof(*objs));
312         avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
313                 objs[i++] = obj;
314                 obj->id = 0;
315         }
316
317         for (n = i, i = 0; i < n; i++)
318                 ubus_add_object(ctx, objs[i]);
319 }
320
321 int ubus_reconnect(struct ubus_context *ctx, const char *path)
322 {
323         struct {
324                 struct ubus_msghdr hdr;
325                 struct blob_attr data;
326         } hdr;
327         struct blob_attr *buf;
328         int ret = UBUS_STATUS_UNKNOWN_ERROR;
329
330         if (!path)
331                 path = UBUS_UNIX_SOCKET;
332
333         if (ctx->sock.fd >= 0) {
334                 if (ctx->sock.registered)
335                         uloop_fd_delete(&ctx->sock);
336
337                 close(ctx->sock.fd);
338         }
339
340         ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
341         if (ctx->sock.fd < 0)
342                 return UBUS_STATUS_CONNECTION_FAILED;
343
344         if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
345                 goto out_close;
346
347         if (!ubus_validate_hdr(&hdr.hdr))
348                 goto out_close;
349
350         if (hdr.hdr.type != UBUS_MSG_HELLO)
351                 goto out_close;
352
353         buf = calloc(1, blob_raw_len(&hdr.data));
354         if (!buf)
355                 goto out_close;
356
357         memcpy(buf, &hdr.data, sizeof(hdr.data));
358         if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
359                 goto out_free;
360
361         ctx->local_id = hdr.hdr.peer;
362         if (!ctx->local_id)
363                 goto out_free;
364
365         ret = UBUS_STATUS_OK;
366         fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK);
367
368         ubus_refresh_state(ctx);
369
370 out_free:
371         free(buf);
372 out_close:
373         if (ret)
374                 close(ctx->sock.fd);
375
376         return ret;
377 }