8 static struct ubus_context
*ctx
= NULL
;
9 static struct blob_buf b
;
17 __DEV_MAX_NOFORCE
= __DEV_MAX
- 1,
20 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
21 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
22 [DEV_FORCE
] = { .name
= "force", .type
= BLOBMSG_TYPE_INT8
},
26 netifd_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
27 struct ubus_request_data
*req
, const char *method
,
28 struct blob_attr
*msg
)
31 struct blob_attr
*tb
[__DEV_MAX
];
32 bool add
= !strncmp(method
, "add", 3);
34 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
37 return UBUS_STATUS_INVALID_ARGUMENT
;
39 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
41 return UBUS_STATUS_NOT_FOUND
;
43 if (!add
|| (tb
[DEV_FORCE
] && blobmsg_get_u8(tb
[DEV_FORCE
])))
44 device_set_present(dev
, add
);
46 device_check_state(dev
);
52 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
53 struct ubus_request_data
*req
, const char *method
,
54 struct blob_attr
*msg
)
61 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
62 struct ubus_request_data
*req
, const char *method
,
63 struct blob_attr
*msg
)
69 static struct ubus_method main_object_methods
[] = {
70 UBUS_METHOD("add_device", netifd_handle_device
, dev_policy
),
71 UBUS_METHOD("remove_device", netifd_handle_device
, dev_policy
),
72 { .name
= "restart", .handler
= netifd_handle_restart
},
73 { .name
= "reload", .handler
= netifd_handle_reload
},
76 static struct ubus_object_type main_object_type
=
77 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
79 static struct ubus_object main_object
= {
80 .name
= "network.interface",
81 .type
= &main_object_type
,
82 .methods
= main_object_methods
,
83 .n_methods
= ARRAY_SIZE(main_object_methods
),
87 netifd_ubus_init(const char *path
)
91 ctx
= ubus_connect(path
);
95 DPRINTF("connected as %08x\n", ctx
->local_id
);
99 ret
= ubus_add_object(ctx
, &main_object
);
101 fprintf(stderr
, "Failed to publish object: %s\n", ubus_strerror(ret
));
107 netifd_ubus_done(void)
113 /* per-interface object */
116 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
117 struct ubus_request_data
*req
, const char *method
,
118 struct blob_attr
*msg
)
120 struct interface
*iface
;
122 iface
= container_of(obj
, struct interface
, ubus
);
123 interface_set_up(iface
);
129 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
130 struct ubus_request_data
*req
, const char *method
,
131 struct blob_attr
*msg
)
133 struct interface
*iface
;
135 iface
= container_of(obj
, struct interface
, ubus
);
136 interface_set_down(iface
);
142 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
144 struct interface_error
*error
;
148 e
= blobmsg_open_array(b
, "errors");
149 list_for_each_entry(error
, &iface
->errors
, list
) {
150 e2
= blobmsg_open_table(b
, NULL
);
152 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
153 blobmsg_add_string(b
, "code", error
->code
);
154 if (error
->data
[0]) {
155 e3
= blobmsg_open_array(b
, "data");
156 for (i
= 0; error
->data
[i
]; i
++)
157 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
158 blobmsg_close_array(b
, e3
);
161 blobmsg_close_table(b
, e2
);
163 blobmsg_close_array(b
, e
);
167 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
168 struct ubus_request_data
*req
, const char *method
,
169 struct blob_attr
*msg
)
171 struct interface
*iface
;
173 iface
= container_of(obj
, struct interface
, ubus
);
175 blob_buf_init(&b
, 0);
176 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
177 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
178 blobmsg_add_u8(&b
, "available", iface
->available
);
179 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
180 if (iface
->main_dev
.dev
) {
181 struct device
*dev
= iface
->main_dev
.dev
;
185 /* use a different field for virtual devices */
191 devinfo
= blobmsg_open_table(&b
, field
);
192 blobmsg_add_string(&b
, "name", dev
->ifname
);
194 if (dev
->type
->dump_status
)
195 dev
->type
->dump_status(dev
, &b
);
197 blobmsg_close_table(&b
, devinfo
);
200 if (!list_is_empty(&iface
->errors
))
201 netifd_add_interface_errors(&b
, iface
);
203 ubus_send_reply(ctx
, req
, b
.head
);
209 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
210 struct ubus_request_data
*req
, const char *method
,
211 struct blob_attr
*msg
)
213 struct interface
*iface
;
214 struct device
*dev
, *main_dev
;
215 struct blob_attr
*tb
[__DEV_MAX
];
216 bool add
= !strncmp(method
, "add", 3);
219 iface
= container_of(obj
, struct interface
, ubus
);
221 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
224 return UBUS_STATUS_INVALID_ARGUMENT
;
226 main_dev
= iface
->main_dev
.dev
;
228 return UBUS_STATUS_NOT_FOUND
;
230 if (!main_dev
->hotplug_ops
)
231 return UBUS_STATUS_NOT_SUPPORTED
;
233 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
);
235 return UBUS_STATUS_NOT_FOUND
;
237 if (main_dev
!= dev
) {
239 ret
= main_dev
->hotplug_ops
->add(main_dev
, dev
);
241 ret
= main_dev
->hotplug_ops
->del(main_dev
, dev
);
243 ret
= UBUS_STATUS_UNKNOWN_ERROR
;
245 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
249 device_free_unused(dev
);
256 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
257 struct ubus_request_data
*req
, const char *method
,
258 struct blob_attr
*msg
)
260 struct interface
*iface
;
262 iface
= container_of(obj
, struct interface
, ubus
);
264 if (!iface
->proto
|| !iface
->proto
->notify
)
265 return UBUS_STATUS_NOT_SUPPORTED
;
267 return iface
->proto
->notify(iface
->proto
, msg
);
271 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
273 struct interface
*iface
;
275 iface
= container_of(timeout
, struct interface
, remove_timer
);
276 vlist_delete(&interfaces
, &iface
->node
);
280 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
281 struct ubus_request_data
*req
, const char *method
,
282 struct blob_attr
*msg
)
284 struct interface
*iface
;
286 iface
= container_of(obj
, struct interface
, ubus
);
287 if (iface
->remove_timer
.cb
)
288 return UBUS_STATUS_INVALID_ARGUMENT
;
290 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
291 uloop_timeout_set(&iface
->remove_timer
, 100);
295 static struct ubus_method iface_object_methods
[] = {
296 { .name
= "up", .handler
= netifd_handle_up
},
297 { .name
= "down", .handler
= netifd_handle_down
},
298 { .name
= "status", .handler
= netifd_handle_status
},
299 { .name
= "add_device", .handler
= netifd_iface_handle_device
,
300 .policy
= dev_policy
, .n_policy
= __DEV_MAX_NOFORCE
},
301 { .name
= "remove_device", .handler
= netifd_iface_handle_device
,
302 .policy
= dev_policy
, .n_policy
= __DEV_MAX_NOFORCE
},
303 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
304 { .name
= "remove", .handler
= netifd_iface_remove
}
307 static struct ubus_object_type iface_object_type
=
308 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
312 netifd_ubus_add_interface(struct interface
*iface
)
314 struct ubus_object
*obj
= &iface
->ubus
;
317 name
= malloc(strlen(main_object
.name
) + strlen(iface
->name
) + 2);
321 sprintf(name
, "%s.%s", main_object
.name
, iface
->name
);
323 obj
->type
= &iface_object_type
;
324 obj
->methods
= iface_object_methods
;
325 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
326 if (ubus_add_object(ctx
, &iface
->ubus
)) {
327 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
334 netifd_ubus_remove_interface(struct interface
*iface
)
336 if (!iface
->ubus
.name
)
339 ubus_remove_object(ctx
, &iface
->ubus
);
340 free((void *) iface
->ubus
.name
);