2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
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
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.
14 #include <arpa/inet.h>
20 static struct ubus_msg_buf *retmsg;
21 static int *retmsg_data;
22 static struct avl_tree clients;
24 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
26 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
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 },
37 static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
39 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
43 static void ubus_msg_close_fd(struct ubus_msg_buf *ub)
52 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
60 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
62 return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
65 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
67 struct ubus_msg_buf *new;
69 new = ubus_msg_from_blob(shared);
73 ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
78 ubus_send_msg_from_blob(struct ubus_client *cl, struct ubus_msg_buf *ub,
81 ub = ubus_reply_from_blob(ub, true);
86 ubus_msg_send(cl, ub, true);
89 static bool ubusd_send_hello(struct ubus_client *cl)
91 struct ubus_msg_buf *ub;
94 ub = ubus_msg_from_blob(true);
98 ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
99 ubus_msg_send(cl, ub, true);
103 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
105 ub->hdr.type = UBUS_MSG_DATA;
106 ubus_msg_send(cl, ub, false);
110 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
112 struct ubus_object *obj;
114 if (!attr[UBUS_ATTR_OBJID])
115 return UBUS_STATUS_INVALID_ARGUMENT;
117 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
119 return UBUS_STATUS_NOT_FOUND;
121 if (obj->client != cl)
122 return UBUS_STATUS_PERMISSION_DENIED;
124 blob_buf_init(&b, 0);
125 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
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);
131 ubusd_free_object(obj);
132 ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
137 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
139 struct ubus_object *obj;
141 obj = ubusd_create_object(cl, attr);
143 return UBUS_STATUS_INVALID_ARGUMENT;
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);
150 ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
154 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
156 struct ubus_method *m;
159 blob_buf_init(&b, 0);
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);
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);
171 ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
174 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
176 struct ubus_object *obj;
181 if (!attr[UBUS_ATTR_OBJPATH]) {
182 avl_for_each_element(&path, obj, path)
183 ubusd_send_obj(cl, ub, obj);
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);
192 return UBUS_STATUS_NOT_FOUND;
194 ubusd_send_obj(cl, ub, obj);
200 obj = avl_find_ge_element(&path, objpath, obj, path);
202 return UBUS_STATUS_NOT_FOUND;
204 while (!strncmp(objpath, obj->path.key, len)) {
206 ubusd_send_obj(cl, ub, obj);
207 if (obj == avl_last_element(&path, obj, path))
209 obj = avl_next_element(obj, path);
213 return UBUS_STATUS_NOT_FOUND;
219 ubusd_forward_invoke(struct ubus_object *obj, const char *method,
220 struct ubus_msg_buf *ub, struct blob_attr *data)
222 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
223 blob_put_string(&b, UBUS_ATTR_METHOD, method);
225 blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
227 ubus_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
230 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
232 struct ubus_object *obj = NULL;
236 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
237 return UBUS_STATUS_INVALID_ARGUMENT;
239 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
241 return UBUS_STATUS_NOT_FOUND;
243 obj = container_of(id, struct ubus_object, id);
245 method = blob_data(attr[UBUS_ATTR_METHOD]);
248 return obj->recv_msg(cl, method, attr[UBUS_ATTR_DATA]);
250 ub->hdr.peer = cl->id.id;
251 blob_buf_init(&b, 0);
252 ubusd_forward_invoke(obj, method, ub, attr[UBUS_ATTR_DATA]);
258 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
260 struct ubus_object *obj = NULL;
261 struct ubus_subscription *s;
264 bool no_reply = false;
267 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
268 return UBUS_STATUS_INVALID_ARGUMENT;
270 if (attr[UBUS_ATTR_NO_REPLY])
271 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
273 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
275 return UBUS_STATUS_NOT_FOUND;
277 obj = container_of(id, struct ubus_object, id);
278 if (obj->client != cl)
279 return UBUS_STATUS_PERMISSION_DENIED;
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);
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);
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);
298 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
299 ubusd_forward_invoke(s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
306 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
308 struct ubus_id *clid;
310 clid = ubus_find_id(&clients, id);
314 return container_of(clid, struct ubus_client, id);
317 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
319 struct ubus_object *obj;
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]))
326 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
330 if (cl != obj->client)
333 cl = ubusd_get_client_by_id(ub->hdr.peer);
337 ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
338 ubus_msg_send(cl, ub, true);
346 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
348 struct ubus_object *obj, *target;
350 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
351 return UBUS_STATUS_INVALID_ARGUMENT;
353 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
355 return UBUS_STATUS_NOT_FOUND;
357 if (cl != obj->client)
358 return UBUS_STATUS_INVALID_ARGUMENT;
360 target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
362 return UBUS_STATUS_NOT_FOUND;
364 if (cl == target->client)
365 return UBUS_STATUS_INVALID_ARGUMENT;
367 ubus_subscribe(obj, target);
371 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
373 struct ubus_object *obj;
374 struct ubus_subscription *s;
377 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
378 return UBUS_STATUS_INVALID_ARGUMENT;
380 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
382 return UBUS_STATUS_NOT_FOUND;
384 if (cl != obj->client)
385 return UBUS_STATUS_INVALID_ARGUMENT;
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)
396 return UBUS_STATUS_NOT_FOUND;
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,
412 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
414 ubus_cmd_cb cb = NULL;
417 retmsg->hdr.seq = ub->hdr.seq;
418 retmsg->hdr.peer = ub->hdr.peer;
420 if (ub->hdr.type < __UBUS_MSG_LAST)
421 cb = handlers[ub->hdr.type];
423 if (ub->hdr.type != UBUS_MSG_STATUS)
424 ubus_msg_close_fd(ub);
427 ret = cb(cl, ub, ubus_parse_msg(ub->data));
429 ret = UBUS_STATUS_INVALID_COMMAND;
436 *retmsg_data = htonl(ret);
437 ubus_msg_send(cl, retmsg, false);
440 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
442 struct ubus_client *cl;
444 cl = calloc(1, sizeof(*cl));
448 INIT_LIST_HEAD(&cl->objects);
451 cl->pending_msg_fd = -1;
453 if (!ubus_alloc_id(&clients, &cl->id, 0))
456 if (!ubusd_send_hello(cl))
462 ubus_free_id(&clients, &cl->id);
468 void ubusd_proto_free_client(struct ubus_client *cl)
470 struct ubus_object *obj;
472 while (!list_empty(&cl->objects)) {
473 obj = list_first_entry(&cl->objects, struct ubus_object, list);
474 ubusd_free_object(obj);
477 ubus_free_id(&clients, &cl->id);
480 void ubus_notify_subscription(struct ubus_object *obj)
482 bool active = !list_empty(&obj->subscribers);
483 struct ubus_msg_buf *ub;
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);
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);
494 void ubus_notify_unsubscribe(struct ubus_subscription *s)
496 struct ubus_msg_buf *ub;
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);
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);
509 static void __init ubusd_proto_init(void)
511 ubus_init_id_tree(&clients);
513 blob_buf_init(&b, 0);
514 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
516 retmsg = ubus_msg_from_blob(false);
520 retmsg->hdr.type = UBUS_MSG_STATUS;
521 retmsg_data = blob_data(blob_data(retmsg->data));