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
);
89 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
90 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
91 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
95 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
96 struct ubus_request_data
*req
, const char *method
,
97 struct blob_attr
*msg
)
99 struct device
*dev
= NULL
;
100 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
101 struct blob_attr
*cur
;
104 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
106 if (!tb
[ALIAS_ATTR_ALIAS
])
107 return UBUS_STATUS_INVALID_ARGUMENT
;
109 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
110 dev
= device_get(blobmsg_data(cur
), true);
112 return UBUS_STATUS_NOT_FOUND
;
115 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
116 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
119 if (!blobmsg_check_attr(cur
, NULL
))
122 alias_notify_device(blobmsg_data(cur
), dev
);
127 device_free_unused(dev
);
128 return UBUS_STATUS_INVALID_ARGUMENT
;
131 static struct ubus_method dev_object_methods
[] = {
132 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
133 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
136 static struct ubus_object_type dev_object_type
=
137 UBUS_OBJECT_TYPE("device", dev_object_methods
);
139 static struct ubus_object dev_object
= {
140 .name
= "network.device",
141 .type
= &dev_object_type
,
142 .methods
= dev_object_methods
,
143 .n_methods
= ARRAY_SIZE(dev_object_methods
),
147 netifd_ubus_init(const char *path
)
151 ctx
= ubus_connect(path
);
155 DPRINTF("connected as %08x\n", ctx
->local_id
);
158 ubus_fd
.fd
= ctx
->sock
.fd
;
159 netifd_fd_add(&ubus_fd
);
161 ret
= ubus_add_object(ctx
, &main_object
);
165 ret
= ubus_add_object(ctx
, &dev_object
);
169 fprintf(stderr
, "Failed to publish object: %s\n", ubus_strerror(ret
));
174 netifd_ubus_done(void)
180 /* per-interface object */
183 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
184 struct ubus_request_data
*req
, const char *method
,
185 struct blob_attr
*msg
)
187 struct interface
*iface
;
189 iface
= container_of(obj
, struct interface
, ubus
);
190 interface_set_up(iface
);
196 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
197 struct ubus_request_data
*req
, const char *method
,
198 struct blob_attr
*msg
)
200 struct interface
*iface
;
202 iface
= container_of(obj
, struct interface
, ubus
);
203 interface_set_down(iface
);
209 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
211 struct interface_error
*error
;
215 e
= blobmsg_open_array(b
, "errors");
216 list_for_each_entry(error
, &iface
->errors
, list
) {
217 e2
= blobmsg_open_table(b
, NULL
);
219 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
220 blobmsg_add_string(b
, "code", error
->code
);
221 if (error
->data
[0]) {
222 e3
= blobmsg_open_array(b
, "data");
223 for (i
= 0; error
->data
[i
]; i
++)
224 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
225 blobmsg_close_array(b
, e3
);
228 blobmsg_close_table(b
, e2
);
230 blobmsg_close_array(b
, e
);
234 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
235 struct ubus_request_data
*req
, const char *method
,
236 struct blob_attr
*msg
)
238 struct interface
*iface
;
240 iface
= container_of(obj
, struct interface
, ubus
);
242 blob_buf_init(&b
, 0);
243 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
244 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
245 blobmsg_add_u8(&b
, "available", iface
->available
);
246 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
248 if (iface
->state
== IFS_UP
) {
249 time_t cur
= system_get_rtime();
250 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
251 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
->dev
->ifname
);
254 if (!(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
255 blobmsg_add_string(&b
, "device", iface
->main_dev
.dev
->ifname
);
257 if (!list_is_empty(&iface
->errors
))
258 netifd_add_interface_errors(&b
, iface
);
260 ubus_send_reply(ctx
, req
, b
.head
);
266 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
267 struct ubus_request_data
*req
, const char *method
,
268 struct blob_attr
*msg
)
270 struct interface
*iface
;
271 struct device
*dev
, *main_dev
;
272 struct blob_attr
*tb
[__DEV_MAX
];
273 bool add
= !strncmp(method
, "add", 3);
276 iface
= container_of(obj
, struct interface
, ubus
);
278 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
281 return UBUS_STATUS_INVALID_ARGUMENT
;
283 main_dev
= iface
->main_dev
.dev
;
285 return UBUS_STATUS_NOT_FOUND
;
287 if (!main_dev
->hotplug_ops
)
288 return UBUS_STATUS_NOT_SUPPORTED
;
290 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
);
292 return UBUS_STATUS_NOT_FOUND
;
294 if (main_dev
!= dev
) {
296 ret
= main_dev
->hotplug_ops
->add(main_dev
, dev
);
298 ret
= main_dev
->hotplug_ops
->del(main_dev
, dev
);
300 ret
= UBUS_STATUS_UNKNOWN_ERROR
;
302 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
306 device_free_unused(dev
);
313 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
314 struct ubus_request_data
*req
, const char *method
,
315 struct blob_attr
*msg
)
317 struct interface
*iface
;
319 iface
= container_of(obj
, struct interface
, ubus
);
321 if (!iface
->proto
|| !iface
->proto
->notify
)
322 return UBUS_STATUS_NOT_SUPPORTED
;
324 return iface
->proto
->notify(iface
->proto
, msg
);
328 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
330 struct interface
*iface
;
332 iface
= container_of(timeout
, struct interface
, remove_timer
);
333 vlist_delete(&interfaces
, &iface
->node
);
337 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
338 struct ubus_request_data
*req
, const char *method
,
339 struct blob_attr
*msg
)
341 struct interface
*iface
;
343 iface
= container_of(obj
, struct interface
, ubus
);
344 if (iface
->remove_timer
.cb
)
345 return UBUS_STATUS_INVALID_ARGUMENT
;
347 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
348 uloop_timeout_set(&iface
->remove_timer
, 100);
352 static struct ubus_method iface_object_methods
[] = {
353 { .name
= "up", .handler
= netifd_handle_up
},
354 { .name
= "down", .handler
= netifd_handle_down
},
355 { .name
= "status", .handler
= netifd_handle_status
},
356 { .name
= "add_device", .handler
= netifd_iface_handle_device
,
357 .policy
= dev_policy
, .n_policy
= __DEV_MAX
},
358 { .name
= "remove_device", .handler
= netifd_iface_handle_device
,
359 .policy
= dev_policy
, .n_policy
= __DEV_MAX
},
360 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
361 { .name
= "remove", .handler
= netifd_iface_remove
}
364 static struct ubus_object_type iface_object_type
=
365 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
369 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
371 blob_buf_init(&b
, 0);
372 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
373 blobmsg_add_string(&b
, "interface", iface
->name
);
374 ubus_send_event(ctx
, "network.interface", b
.head
);
378 netifd_ubus_add_interface(struct interface
*iface
)
380 struct ubus_object
*obj
= &iface
->ubus
;
383 asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
);
388 obj
->type
= &iface_object_type
;
389 obj
->methods
= iface_object_methods
;
390 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
391 if (ubus_add_object(ctx
, &iface
->ubus
)) {
392 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
399 netifd_ubus_remove_interface(struct interface
*iface
)
401 if (!iface
->ubus
.name
)
404 ubus_remove_object(ctx
, &iface
->ubus
);
405 free((void *) iface
->ubus
.name
);