libubus: rework buffer allocation code
[project/ubus.git] / ubusd_proto.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 <arpa/inet.h>
15 #include <unistd.h>
16
17 #include "ubusd.h"
18
19 struct blob_buf b;
20 static struct ubus_msg_buf *retmsg;
21 static int *retmsg_data;
22 static struct avl_tree clients;
23
24 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
25
26 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
27
28 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
29         [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
30         [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
31         [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
32         [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
33         [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
34         [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
35 };
36
37 static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
38 {
39         blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
40         return attrbuf;
41 }
42
43 static void ubus_msg_close_fd(struct ubus_msg_buf *ub)
44 {
45         if (ub->fd < 0)
46                 return;
47
48         close(ub->fd);
49         ub->fd = -1;
50 }
51
52 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
53 {
54         ub->hdr.version = 0;
55         ub->hdr.type = type;
56         ub->hdr.seq = seq;
57         ub->hdr.peer = peer;
58 }
59
60 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
61 {
62         return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
63 }
64
65 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
66 {
67         struct ubus_msg_buf *new;
68
69         new = ubus_msg_from_blob(shared);
70         if (!new)
71                 return NULL;
72
73         ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
74         return new;
75 }
76
77 static void
78 ubus_send_msg_from_blob(struct ubus_client *cl, struct ubus_msg_buf *ub,
79                         uint8_t type)
80 {
81         ub = ubus_reply_from_blob(ub, true);
82         if (!ub)
83                 return;
84
85         ub->hdr.type = type;
86         ubus_msg_send(cl, ub, true);
87 }
88
89 static bool ubusd_send_hello(struct ubus_client *cl)
90 {
91         struct ubus_msg_buf *ub;
92
93         blob_buf_init(&b, 0);
94         ub = ubus_msg_from_blob(true);
95         if (!ub)
96                 return false;
97
98         ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
99         ubus_msg_send(cl, ub, true);
100         return true;
101 }
102
103 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
104 {
105         ub->hdr.type = UBUS_MSG_DATA;
106         ubus_msg_send(cl, ub, false);
107         return 0;
108 }
109
110 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
111 {
112         struct ubus_object *obj;
113
114         if (!attr[UBUS_ATTR_OBJID])
115                 return UBUS_STATUS_INVALID_ARGUMENT;
116
117         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
118         if (!obj)
119                 return UBUS_STATUS_NOT_FOUND;
120
121         if (obj->client != cl)
122                 return UBUS_STATUS_PERMISSION_DENIED;
123
124         blob_buf_init(&b, 0);
125         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
126
127         /* check if we're removing the object type as well */
128         if (obj->type && obj->type->refcount == 1)
129                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
130
131         ubusd_free_object(obj);
132         ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
133
134         return 0;
135 }
136
137 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
138 {
139         struct ubus_object *obj;
140
141         obj = ubusd_create_object(cl, attr);
142         if (!obj)
143                 return UBUS_STATUS_INVALID_ARGUMENT;
144
145         blob_buf_init(&b, 0);
146         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
147         if (attr[UBUS_ATTR_SIGNATURE])
148                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
149
150         ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
151         return 0;
152 }
153
154 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
155 {
156         struct ubus_method *m;
157         void *s;
158
159         blob_buf_init(&b, 0);
160
161         if (obj->path.key)
162                 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
163         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
164         blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
165
166         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
167         list_for_each_entry(m, &obj->type->methods, list)
168                 blobmsg_add_blob(&b, m->data);
169         blob_nest_end(&b, s);
170
171         ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
172 }
173
174 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
175 {
176         struct ubus_object *obj;
177         char *objpath;
178         bool found = false;
179         int len;
180
181         if (!attr[UBUS_ATTR_OBJPATH]) {
182                 avl_for_each_element(&path, obj, path)
183                         ubusd_send_obj(cl, ub, obj);
184                 return 0;
185         }
186
187         objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
188         len = strlen(objpath);
189         if (objpath[len - 1] != '*') {
190                 obj = avl_find_element(&path, objpath, obj, path);
191                 if (!obj)
192                         return UBUS_STATUS_NOT_FOUND;
193
194                 ubusd_send_obj(cl, ub, obj);
195                 return 0;
196         }
197
198         objpath[--len] = 0;
199
200         obj = avl_find_ge_element(&path, objpath, obj, path);
201         if (!obj)
202                 return UBUS_STATUS_NOT_FOUND;
203
204         while (!strncmp(objpath, obj->path.key, len)) {
205                 found = true;
206                 ubusd_send_obj(cl, ub, obj);
207                 if (obj == avl_last_element(&path, obj, path))
208                         break;
209                 obj = avl_next_element(obj, path);
210         }
211
212         if (!found)
213                 return UBUS_STATUS_NOT_FOUND;
214
215         return 0;
216 }
217
218 static void
219 ubusd_forward_invoke(struct ubus_object *obj, const char *method,
220                      struct ubus_msg_buf *ub, struct blob_attr *data)
221 {
222         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
223         blob_put_string(&b, UBUS_ATTR_METHOD, method);
224         if (data)
225                 blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
226
227         ubus_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
228 }
229
230 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
231 {
232         struct ubus_object *obj = NULL;
233         struct ubus_id *id;
234         const char *method;
235
236         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
237                 return UBUS_STATUS_INVALID_ARGUMENT;
238
239         id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
240         if (!id)
241                 return UBUS_STATUS_NOT_FOUND;
242
243         obj = container_of(id, struct ubus_object, id);
244
245         method = blob_data(attr[UBUS_ATTR_METHOD]);
246
247         if (!obj->client)
248                 return obj->recv_msg(cl, method, attr[UBUS_ATTR_DATA]);
249
250         ub->hdr.peer = cl->id.id;
251         blob_buf_init(&b, 0);
252         ubusd_forward_invoke(obj, method, ub, attr[UBUS_ATTR_DATA]);
253         ubus_msg_free(ub);
254
255         return -1;
256 }
257
258 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
259 {
260         struct ubus_object *obj = NULL;
261         struct ubus_subscription *s;
262         struct ubus_id *id;
263         const char *method;
264         bool no_reply = false;
265         void *c;
266
267         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
268                 return UBUS_STATUS_INVALID_ARGUMENT;
269
270         if (attr[UBUS_ATTR_NO_REPLY])
271                 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
272
273         id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
274         if (!id)
275                 return UBUS_STATUS_NOT_FOUND;
276
277         obj = container_of(id, struct ubus_object, id);
278         if (obj->client != cl)
279                 return UBUS_STATUS_PERMISSION_DENIED;
280
281         if (!no_reply) {
282                 blob_buf_init(&b, 0);
283                 blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
284                 c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
285                 list_for_each_entry(s, &obj->subscribers, list) {
286                         blob_put_int32(&b, 0, s->subscriber->id.id);
287                 }
288                 blob_nest_end(&b, c);
289                 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
290                 ubus_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
291         }
292
293         ub->hdr.peer = cl->id.id;
294         method = blob_data(attr[UBUS_ATTR_METHOD]);
295         list_for_each_entry(s, &obj->subscribers, list) {
296                 blob_buf_init(&b, 0);
297                 if (no_reply)
298                         blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
299                 ubusd_forward_invoke(s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
300         }
301         ubus_msg_free(ub);
302
303         return -1;
304 }
305
306 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
307 {
308         struct ubus_id *clid;
309
310         clid = ubus_find_id(&clients, id);
311         if (!clid)
312                 return NULL;
313
314         return container_of(clid, struct ubus_client, id);
315 }
316
317 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
318 {
319         struct ubus_object *obj;
320
321         if (!attr[UBUS_ATTR_OBJID] ||
322             (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
323             (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
324                 goto error;
325
326         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
327         if (!obj)
328                 goto error;
329
330         if (cl != obj->client)
331                 goto error;
332
333         cl = ubusd_get_client_by_id(ub->hdr.peer);
334         if (!cl)
335                 goto error;
336
337         ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
338         ubus_msg_send(cl, ub, true);
339         return -1;
340
341 error:
342         ubus_msg_free(ub);
343         return -1;
344 }
345
346 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
347 {
348         struct ubus_object *obj, *target;
349
350         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
351                 return UBUS_STATUS_INVALID_ARGUMENT;
352
353         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
354         if (!obj)
355                 return UBUS_STATUS_NOT_FOUND;
356
357         if (cl != obj->client)
358                 return UBUS_STATUS_INVALID_ARGUMENT;
359
360         target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
361         if (!target)
362                 return UBUS_STATUS_NOT_FOUND;
363
364         if (cl == target->client)
365                 return UBUS_STATUS_INVALID_ARGUMENT;
366
367         ubus_subscribe(obj, target);
368         return 0;
369 }
370
371 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
372 {
373         struct ubus_object *obj;
374         struct ubus_subscription *s;
375         uint32_t id;
376
377         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
378                 return UBUS_STATUS_INVALID_ARGUMENT;
379
380         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
381         if (!obj)
382                 return UBUS_STATUS_NOT_FOUND;
383
384         if (cl != obj->client)
385                 return UBUS_STATUS_INVALID_ARGUMENT;
386
387         id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
388         list_for_each_entry(s, &obj->target_list, target_list) {
389                 if (s->target->id.id != id)
390                         continue;
391
392                 ubus_unsubscribe(s);
393                 return 0;
394         }
395
396         return UBUS_STATUS_NOT_FOUND;
397 }
398
399 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
400         [UBUS_MSG_PING] = ubusd_send_pong,
401         [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
402         [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
403         [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
404         [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
405         [UBUS_MSG_STATUS] = ubusd_handle_response,
406         [UBUS_MSG_DATA] = ubusd_handle_response,
407         [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
408         [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
409         [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
410 };
411
412 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
413 {
414         ubus_cmd_cb cb = NULL;
415         int ret;
416
417         retmsg->hdr.seq = ub->hdr.seq;
418         retmsg->hdr.peer = ub->hdr.peer;
419
420         if (ub->hdr.type < __UBUS_MSG_LAST)
421                 cb = handlers[ub->hdr.type];
422
423         if (ub->hdr.type != UBUS_MSG_STATUS)
424                 ubus_msg_close_fd(ub);
425
426         if (cb)
427                 ret = cb(cl, ub, ubus_parse_msg(ub->data));
428         else
429                 ret = UBUS_STATUS_INVALID_COMMAND;
430
431         if (ret == -1)
432                 return;
433
434         ubus_msg_free(ub);
435
436         *retmsg_data = htonl(ret);
437         ubus_msg_send(cl, retmsg, false);
438 }
439
440 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
441 {
442         struct ubus_client *cl;
443
444         cl = calloc(1, sizeof(*cl));
445         if (!cl)
446                 return NULL;
447
448         INIT_LIST_HEAD(&cl->objects);
449         cl->sock.fd = fd;
450         cl->sock.cb = cb;
451         cl->pending_msg_fd = -1;
452
453         if (!ubus_alloc_id(&clients, &cl->id, 0))
454                 goto free;
455
456         if (!ubusd_send_hello(cl))
457                 goto delete;
458
459         return cl;
460
461 delete:
462         ubus_free_id(&clients, &cl->id);
463 free:
464         free(cl);
465         return NULL;
466 }
467
468 void ubusd_proto_free_client(struct ubus_client *cl)
469 {
470         struct ubus_object *obj;
471
472         while (!list_empty(&cl->objects)) {
473                 obj = list_first_entry(&cl->objects, struct ubus_object, list);
474                 ubusd_free_object(obj);
475         }
476
477         ubus_free_id(&clients, &cl->id);
478 }
479
480 void ubus_notify_subscription(struct ubus_object *obj)
481 {
482         bool active = !list_empty(&obj->subscribers);
483         struct ubus_msg_buf *ub;
484
485         blob_buf_init(&b, 0);
486         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
487         blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
488
489         ub = ubus_msg_from_blob(false);
490         ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
491         ubus_msg_send(obj->client, ub, true);
492 }
493
494 void ubus_notify_unsubscribe(struct ubus_subscription *s)
495 {
496         struct ubus_msg_buf *ub;
497
498         blob_buf_init(&b, 0);
499         blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
500         blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
501
502         ub = ubus_msg_from_blob(false);
503         ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
504         ubus_msg_send(s->subscriber->client, ub, true);
505
506         ubus_unsubscribe(s);
507 }
508
509 static void __init ubusd_proto_init(void)
510 {
511         ubus_init_id_tree(&clients);
512
513         blob_buf_init(&b, 0);
514         blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
515
516         retmsg = ubus_msg_from_blob(false);
517         if (!retmsg)
518                 exit(1);
519
520         retmsg->hdr.type = UBUS_MSG_STATUS;
521         retmsg_data = blob_data(blob_data(retmsg->data));
522 }