12 static struct ubus_context
*ctx
= NULL
;
13 static struct blob_buf b
;
14 static struct netifd_fd ubus_fd
;
19 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
20 struct ubus_request_data
*req
, const char *method
,
21 struct blob_attr
*msg
)
28 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
29 struct ubus_request_data
*req
, const char *method
,
30 struct blob_attr
*msg
)
36 static struct ubus_method main_object_methods
[] = {
37 { .name
= "restart", .handler
= netifd_handle_restart
},
38 { .name
= "reload", .handler
= netifd_handle_reload
},
41 static struct ubus_object_type main_object_type
=
42 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
44 static struct ubus_object main_object
= {
46 .type
= &main_object_type
,
47 .methods
= main_object_methods
,
48 .n_methods
= ARRAY_SIZE(main_object_methods
),
56 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
57 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
61 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
62 struct ubus_request_data
*req
, const char *method
,
63 struct blob_attr
*msg
)
65 struct device
*dev
= NULL
;
66 struct blob_attr
*tb
[__DEV_MAX
];
68 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
71 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
73 return UBUS_STATUS_INVALID_ARGUMENT
;
77 device_dump_status(&b
, dev
);
78 ubus_send_reply(ctx
, req
, b
.head
);
83 static struct ubus_method dev_object_methods
[] = {
84 UBUS_METHOD("status", netifd_dev_status
, dev_policy
)
87 static struct ubus_object_type dev_object_type
=
88 UBUS_OBJECT_TYPE("device", dev_object_methods
);
90 static struct ubus_object dev_object
= {
91 .name
= "network.device",
92 .type
= &dev_object_type
,
93 .methods
= dev_object_methods
,
94 .n_methods
= ARRAY_SIZE(dev_object_methods
),
98 netifd_ubus_init(const char *path
)
102 ctx
= ubus_connect(path
);
106 DPRINTF("connected as %08x\n", ctx
->local_id
);
109 ubus_fd
.fd
= ctx
->sock
.fd
;
110 netifd_fd_add(&ubus_fd
);
112 ret
= ubus_add_object(ctx
, &main_object
);
116 ret
= ubus_add_object(ctx
, &dev_object
);
120 fprintf(stderr
, "Failed to publish object: %s\n", ubus_strerror(ret
));
125 netifd_ubus_done(void)
131 /* per-interface object */
134 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
135 struct ubus_request_data
*req
, const char *method
,
136 struct blob_attr
*msg
)
138 struct interface
*iface
;
140 iface
= container_of(obj
, struct interface
, ubus
);
141 interface_set_up(iface
);
147 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
148 struct ubus_request_data
*req
, const char *method
,
149 struct blob_attr
*msg
)
151 struct interface
*iface
;
153 iface
= container_of(obj
, struct interface
, ubus
);
154 interface_set_down(iface
);
160 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
162 struct interface_error
*error
;
166 e
= blobmsg_open_array(b
, "errors");
167 list_for_each_entry(error
, &iface
->errors
, list
) {
168 e2
= blobmsg_open_table(b
, NULL
);
170 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
171 blobmsg_add_string(b
, "code", error
->code
);
172 if (error
->data
[0]) {
173 e3
= blobmsg_open_array(b
, "data");
174 for (i
= 0; error
->data
[i
]; i
++)
175 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
176 blobmsg_close_array(b
, e3
);
179 blobmsg_close_table(b
, e2
);
181 blobmsg_close_array(b
, e
);
185 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
186 struct ubus_request_data
*req
, const char *method
,
187 struct blob_attr
*msg
)
189 struct interface
*iface
;
191 iface
= container_of(obj
, struct interface
, ubus
);
193 blob_buf_init(&b
, 0);
194 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
195 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
196 blobmsg_add_u8(&b
, "available", iface
->available
);
197 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
199 if (iface
->state
== IFS_UP
) {
200 time_t cur
= system_get_rtime();
201 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
202 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
->dev
->ifname
);
205 if (!(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
206 blobmsg_add_string(&b
, "device", iface
->main_dev
.dev
->ifname
);
208 if (!list_is_empty(&iface
->errors
))
209 netifd_add_interface_errors(&b
, iface
);
211 ubus_send_reply(ctx
, req
, b
.head
);
217 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
218 struct ubus_request_data
*req
, const char *method
,
219 struct blob_attr
*msg
)
221 struct interface
*iface
;
222 struct device
*dev
, *main_dev
;
223 struct blob_attr
*tb
[__DEV_MAX
];
224 bool add
= !strncmp(method
, "add", 3);
227 iface
= container_of(obj
, struct interface
, ubus
);
229 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
232 return UBUS_STATUS_INVALID_ARGUMENT
;
234 main_dev
= iface
->main_dev
.dev
;
236 return UBUS_STATUS_NOT_FOUND
;
238 if (!main_dev
->hotplug_ops
)
239 return UBUS_STATUS_NOT_SUPPORTED
;
241 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
);
243 return UBUS_STATUS_NOT_FOUND
;
245 if (main_dev
!= dev
) {
247 ret
= main_dev
->hotplug_ops
->add(main_dev
, dev
);
249 ret
= main_dev
->hotplug_ops
->del(main_dev
, dev
);
251 ret
= UBUS_STATUS_UNKNOWN_ERROR
;
253 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
257 device_free_unused(dev
);
264 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
265 struct ubus_request_data
*req
, const char *method
,
266 struct blob_attr
*msg
)
268 struct interface
*iface
;
270 iface
= container_of(obj
, struct interface
, ubus
);
272 if (!iface
->proto
|| !iface
->proto
->notify
)
273 return UBUS_STATUS_NOT_SUPPORTED
;
275 return iface
->proto
->notify(iface
->proto
, msg
);
279 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
281 struct interface
*iface
;
283 iface
= container_of(timeout
, struct interface
, remove_timer
);
284 vlist_delete(&interfaces
, &iface
->node
);
288 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
289 struct ubus_request_data
*req
, const char *method
,
290 struct blob_attr
*msg
)
292 struct interface
*iface
;
294 iface
= container_of(obj
, struct interface
, ubus
);
295 if (iface
->remove_timer
.cb
)
296 return UBUS_STATUS_INVALID_ARGUMENT
;
298 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
299 uloop_timeout_set(&iface
->remove_timer
, 100);
303 static struct ubus_method iface_object_methods
[] = {
304 { .name
= "up", .handler
= netifd_handle_up
},
305 { .name
= "down", .handler
= netifd_handle_down
},
306 { .name
= "status", .handler
= netifd_handle_status
},
307 { .name
= "add_device", .handler
= netifd_iface_handle_device
,
308 .policy
= dev_policy
, .n_policy
= __DEV_MAX
},
309 { .name
= "remove_device", .handler
= netifd_iface_handle_device
,
310 .policy
= dev_policy
, .n_policy
= __DEV_MAX
},
311 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
312 { .name
= "remove", .handler
= netifd_iface_remove
}
315 static struct ubus_object_type iface_object_type
=
316 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
320 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
322 blob_buf_init(&b
, 0);
323 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
324 blobmsg_add_string(&b
, "interface", iface
->name
);
325 ubus_send_event(ctx
, "network.interface", b
.head
);
329 netifd_ubus_add_interface(struct interface
*iface
)
331 struct ubus_object
*obj
= &iface
->ubus
;
334 asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
);
339 obj
->type
= &iface_object_type
;
340 obj
->methods
= iface_object_methods
;
341 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
342 if (ubus_add_object(ctx
, &iface
->ubus
)) {
343 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
350 netifd_ubus_remove_interface(struct interface
*iface
)
352 if (!iface
->ubus
.name
)
355 ubus_remove_object(ctx
, &iface
->ubus
);
356 free((void *) iface
->ubus
.name
);