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 check_device_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
)
60 static struct ubus_method main_object_methods
[] = {
61 UBUS_METHOD("add_device", netifd_handle_device
, dev_policy
),
62 UBUS_METHOD("remove_device", netifd_handle_device
, dev_policy
),
63 { .name
= "restart", .handler
= netifd_handle_restart
},
66 static struct ubus_object_type main_object_type
=
67 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
69 static struct ubus_object main_object
= {
70 .name
= "network.interface",
71 .type
= &main_object_type
,
72 .methods
= main_object_methods
,
73 .n_methods
= ARRAY_SIZE(main_object_methods
),
77 netifd_ubus_init(const char *path
)
81 ctx
= ubus_connect(path
);
85 DPRINTF("connected as %08x\n", ctx
->local_id
);
89 ret
= ubus_add_object(ctx
, &main_object
);
91 fprintf(stderr
, "Failed to publish object: %s\n", ubus_strerror(ret
));
97 netifd_ubus_done(void)
103 /* per-interface object */
106 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
107 struct ubus_request_data
*req
, const char *method
,
108 struct blob_attr
*msg
)
110 struct interface
*iface
;
112 iface
= container_of(obj
, struct interface
, ubus
);
113 interface_set_up(iface
);
119 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
120 struct ubus_request_data
*req
, const char *method
,
121 struct blob_attr
*msg
)
123 struct interface
*iface
;
125 iface
= container_of(obj
, struct interface
, ubus
);
126 interface_set_down(iface
);
132 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
134 struct interface_error
*error
;
138 e
= blobmsg_open_array(b
, "errors");
139 list_for_each_entry(error
, &iface
->errors
, list
) {
140 e2
= blobmsg_open_table(b
, NULL
);
142 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
143 blobmsg_add_string(b
, "code", error
->code
);
144 if (error
->data
[0]) {
145 e3
= blobmsg_open_array(b
, "data");
146 for (i
= 0; error
->data
[i
]; i
++)
147 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
148 blobmsg_close_array(b
, e3
);
151 blobmsg_close_table(b
, e2
);
153 blobmsg_close_array(b
, e
);
157 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
158 struct ubus_request_data
*req
, const char *method
,
159 struct blob_attr
*msg
)
161 struct interface
*iface
;
163 iface
= container_of(obj
, struct interface
, ubus
);
165 blob_buf_init(&b
, 0);
166 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
167 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
168 blobmsg_add_u8(&b
, "available", iface
->available
);
169 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
170 if (iface
->main_dev
.dev
) {
171 struct device
*dev
= iface
->main_dev
.dev
;
175 /* use a different field for virtual devices */
181 devinfo
= blobmsg_open_table(&b
, field
);
182 blobmsg_add_string(&b
, "name", dev
->ifname
);
184 if (dev
->type
->dump_status
)
185 dev
->type
->dump_status(dev
, &b
);
187 blobmsg_close_table(&b
, devinfo
);
190 if (!list_is_empty(&iface
->errors
))
191 netifd_add_interface_errors(&b
, iface
);
193 ubus_send_reply(ctx
, req
, b
.head
);
199 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
200 struct ubus_request_data
*req
, const char *method
,
201 struct blob_attr
*msg
)
203 struct interface
*iface
;
204 struct device
*dev
, *main_dev
;
205 struct blob_attr
*tb
[__DEV_MAX
];
206 bool add
= !strncmp(method
, "add", 3);
209 iface
= container_of(obj
, struct interface
, ubus
);
211 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
214 return UBUS_STATUS_INVALID_ARGUMENT
;
216 main_dev
= iface
->main_dev
.dev
;
218 return UBUS_STATUS_NOT_FOUND
;
220 if (!main_dev
->hotplug_ops
)
221 return UBUS_STATUS_NOT_SUPPORTED
;
223 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
);
225 return UBUS_STATUS_NOT_FOUND
;
227 if (main_dev
!= dev
) {
229 ret
= main_dev
->hotplug_ops
->add(main_dev
, dev
);
231 ret
= main_dev
->hotplug_ops
->del(main_dev
, dev
);
233 ret
= UBUS_STATUS_UNKNOWN_ERROR
;
235 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
239 device_free_unused(dev
);
246 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
247 struct ubus_request_data
*req
, const char *method
,
248 struct blob_attr
*msg
)
250 struct interface
*iface
;
252 iface
= container_of(obj
, struct interface
, ubus
);
254 if (!iface
->proto
|| !iface
->proto
->notify
)
255 return UBUS_STATUS_NOT_SUPPORTED
;
257 return iface
->proto
->notify(iface
->proto
, msg
);
260 static struct ubus_method iface_object_methods
[] = {
261 { .name
= "up", .handler
= netifd_handle_up
},
262 { .name
= "down", .handler
= netifd_handle_down
},
263 { .name
= "status", .handler
= netifd_handle_status
},
264 { .name
= "add_device", .handler
= netifd_iface_handle_device
,
265 .policy
= dev_policy
, .n_policy
= __DEV_MAX_NOFORCE
},
266 { .name
= "remove_device", .handler
= netifd_iface_handle_device
,
267 .policy
= dev_policy
, .n_policy
= __DEV_MAX_NOFORCE
},
268 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
271 static struct ubus_object_type iface_object_type
=
272 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
276 netifd_ubus_add_interface(struct interface
*iface
)
278 struct ubus_object
*obj
= &iface
->ubus
;
281 name
= malloc(strlen(main_object
.name
) + strlen(iface
->name
) + 2);
285 sprintf(name
, "%s.%s", main_object
.name
, iface
->name
);
287 obj
->type
= &iface_object_type
;
288 obj
->methods
= iface_object_methods
;
289 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
290 if (ubus_add_object(ctx
, &iface
->ubus
)) {
291 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
298 netifd_ubus_remove_interface(struct interface
*iface
)
300 if (!iface
->ubus
.name
)
303 ubus_remove_object(ctx
, &iface
->ubus
);
304 free((void *) iface
->ubus
.name
);