cdd1ff0583212766b675aa00f9071ccca565d69b
[project/netifd.git] / ubus.c
1 #include <string.h>
2
3 #include "netifd.h"
4 #include "ubus.h"
5
6 static struct ubus_context *ctx = NULL;
7 static struct blob_buf b;
8
9 /* global object */
10
11 enum {
12         DEV_NAME,
13         DEV_FORCE,
14         DEV_LAST,
15 };
16
17 static const struct blobmsg_policy dev_policy[] = {
18         [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
19         [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
20 };
21
22 static int netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
23                                 struct ubus_request_data *req, const char *method,
24                                 struct blob_attr *msg)
25 {
26         struct device *dev;
27         struct blob_attr *tb[DEV_LAST];
28         bool add = !strncmp(method, "add", 3);
29
30         blobmsg_parse(dev_policy, ARRAY_SIZE(dev_policy), tb, blob_data(msg), blob_len(msg));
31
32         if (!tb[DEV_NAME])
33                 return UBUS_STATUS_INVALID_ARGUMENT;
34
35         dev = get_device(blobmsg_data(tb[DEV_NAME]), false);
36         if (!dev)
37                 return UBUS_STATUS_NOT_FOUND;
38
39         if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
40                 set_device_present(dev, add);
41         else
42                 check_device_state(dev);
43
44         return 0;
45 }
46
47 static struct ubus_method main_object_methods[] = {
48         UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
49         UBUS_METHOD("del_device", netifd_handle_device, dev_policy),
50 };
51
52 static struct ubus_object_type main_object_type =
53         UBUS_OBJECT_TYPE("netifd", main_object_methods);
54
55 static struct ubus_object main_object = {
56         .name = "network.interface",
57         .type = &main_object_type,
58         .methods = main_object_methods,
59         .n_methods = ARRAY_SIZE(main_object_methods),
60 };
61
62 int netifd_ubus_init(const char *path)
63 {
64         int ret;
65
66         ctx = ubus_connect(path);
67         if (!ctx)
68                 return -EIO;
69
70         DPRINTF("connected as %08x\n", ctx->local_id);
71         uloop_init();
72         ubus_add_uloop(ctx);
73
74         ret = ubus_add_object(ctx, &main_object);
75         if (ret != 0)
76                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
77
78         return 0;
79 }
80
81 void netifd_ubus_done(void)
82 {
83         ubus_free(ctx);
84 }
85
86
87 /* per-interface object */
88
89 static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
90                             struct ubus_request_data *req, const char *method,
91                             struct blob_attr *msg)
92 {
93         struct interface *iface;
94
95         iface = container_of(obj, struct interface, ubus);
96         set_interface_up(iface);
97
98         return 0;
99 }
100
101 static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
102                               struct ubus_request_data *req, const char *method,
103                               struct blob_attr *msg)
104 {
105         struct interface *iface;
106
107         iface = container_of(obj, struct interface, ubus);
108         set_interface_down(iface);
109
110         return 0;
111 }
112
113 static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
114                                 struct ubus_request_data *req, const char *method,
115                                 struct blob_attr *msg)
116 {
117         struct interface *iface;
118
119         iface = container_of(obj, struct interface, ubus);
120
121         blob_buf_init(&b, 0);
122         blobmsg_add_u8(&b, "up", iface->up);
123         blobmsg_add_u8(&b, "active", iface->active);
124         blobmsg_add_u8(&b, "autostart", iface->autostart);
125         if (iface->main_dev.dev) {
126                 struct device *dev = iface->main_dev.dev;
127                 const char *field;
128                 void *devinfo;
129
130                 /* use a different field for virtual devices */
131                 if (dev->avl.key)
132                         field = "device";
133                 else
134                         field = "link";
135
136                 devinfo = blobmsg_open_table(&b, field);
137                 blobmsg_add_string(&b, "name", dev->ifname);
138
139                 if (dev->type->dump_status)
140                         dev->type->dump_status(dev, &b);
141
142                 blobmsg_close_table(&b, devinfo);
143         }
144
145         ubus_send_reply(ctx, req, b.head);
146
147         return 0;
148 }
149
150
151 static struct ubus_method iface_object_methods[] = {
152         { .name = "up", .handler = netifd_handle_up },
153         { .name = "down", .handler = netifd_handle_down },
154         { .name = "status", .handler = netifd_handle_status },
155 };
156
157 static struct ubus_object_type iface_object_type =
158         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
159
160
161 void netifd_ubus_add_interface(struct interface *iface)
162 {
163         struct ubus_object *obj = &iface->ubus;
164         char *name;
165
166         name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
167         if (!name)
168                 return;
169
170         sprintf(name, "%s.%s", main_object.name, iface->name);
171         obj->name = name;
172         obj->type = &iface_object_type;
173         obj->methods = iface_object_methods;
174         obj->n_methods = ARRAY_SIZE(iface_object_methods);
175         if (ubus_add_object(ctx, &iface->ubus)) {
176                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
177                 free(name);
178                 obj->name = NULL;
179         }
180 }
181
182 void netifd_ubus_remove_interface(struct interface *iface)
183 {
184         if (!iface->ubus.name)
185                 return;
186
187         ubus_remove_object(ctx, &iface->ubus);
188         free((void *) iface->ubus.name);
189 }