8 #include "interface-ip.h"
14 struct vlist_tree interfaces
;
20 IFACE_ATTR_DEFAULTROUTE
,
25 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
26 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
27 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
28 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
29 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
30 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
33 const struct config_param_list interface_attr_list
= {
34 .n_params
= IFACE_ATTR_MAX
,
35 .params
= iface_attrs
,
39 interface_clear_errors(struct interface
*iface
)
41 struct interface_error
*error
, *tmp
;
43 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
44 list_del(&error
->list
);
49 void interface_add_error(struct interface
*iface
, const char *subsystem
,
50 const char *code
, const char **data
, int n_data
)
52 struct interface_error
*error
;
58 len
= n_data
* sizeof(char *);
59 datalen
= alloca(len
);
60 for (i
= 0; i
< n_data
; i
++) {
61 datalen
[i
] = strlen(data
[i
]) + 1;
66 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
70 list_add_tail(&error
->list
, &iface
->errors
);
71 error
->subsystem
= subsystem
;
74 dest
= (char *) &error
->data
[n_data
+ 1];
75 for (i
= 0; i
< n_data
; i
++) {
76 error
->data
[i
] = dest
;
77 memcpy(dest
, data
[i
], datalen
[i
]);
80 error
->data
[n_data
] = NULL
;
84 interface_event(struct interface
*iface
, enum interface_event ev
)
86 struct interface_user
*dep
, *tmp
;
88 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
89 dep
->cb(dep
, IFEV_UP
);
91 interface_queue_event(iface
, ev
);
95 interface_flush_state(struct interface
*iface
)
97 interface_ip_flush(&iface
->proto_ip
);
98 if (iface
->main_dev
.dev
)
99 device_release(&iface
->main_dev
);
100 if (iface
->l3_dev
!= &iface
->main_dev
&& iface
->l3_dev
->dev
)
101 device_release(iface
->l3_dev
);
105 mark_interface_down(struct interface
*iface
)
107 if (iface
->state
== IFS_UP
)
108 interface_event(iface
, IFEV_DOWN
);
109 interface_flush_state(iface
);
110 iface
->state
= IFS_DOWN
;
114 __interface_set_down(struct interface
*iface
, bool force
)
116 interface_clear_errors(iface
);
118 if (iface
->state
== IFS_DOWN
||
119 iface
->state
== IFS_TEARDOWN
)
122 if (iface
->state
== IFS_UP
)
123 interface_event(iface
, IFEV_DOWN
);
124 iface
->state
= IFS_TEARDOWN
;
125 interface_ip_flush(&iface
->config_ip
);
126 interface_ip_flush(&iface
->proto_ip
);
127 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
129 interface_flush_state(iface
);
133 interface_cb(struct device_user
*dep
, enum device_event ev
)
135 struct interface
*iface
;
138 iface
= container_of(dep
, struct interface
, main_dev
);
143 case DEV_EVENT_REMOVE
:
150 interface_set_available(iface
, new_state
);
154 interface_set_available(struct interface
*iface
, bool new_state
)
156 if (iface
->available
== new_state
)
159 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
160 iface
->available
= new_state
;
163 if (iface
->autostart
&& !config_init
)
164 interface_set_up(iface
);
166 __interface_set_down(iface
, true);
170 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
173 list_add(&dep
->list
, &iface
->users
);
174 if (iface
->state
== IFS_UP
)
175 dep
->cb(dep
, IFEV_UP
);
179 interface_remove_user(struct interface_user
*dep
)
181 list_del_init(&dep
->list
);
186 interface_claim_device(struct interface
*iface
)
191 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
192 dev
= device_get(iface
->ifname
, true);
194 device_add_user(&iface
->main_dev
, dev
);
196 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
197 interface_set_available(iface
, true);
202 interface_cleanup(struct interface
*iface
, bool reload
)
204 struct interface_user
*dep
, *tmp
;
206 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
207 interface_remove_user(dep
);
209 interface_ip_flush(&iface
->config_ip
);
210 interface_flush_state(iface
);
211 interface_clear_errors(iface
);
212 if (iface
->main_dev
.dev
&&
213 (!reload
|| !iface
->main_dev
.hotplug
))
214 device_remove_user(&iface
->main_dev
);
215 iface
->l3_dev
= &iface
->main_dev
;
216 interface_set_proto_state(iface
, NULL
);
220 interface_do_free(struct interface
*iface
)
222 interface_cleanup(iface
, false);
224 netifd_ubus_remove_interface(iface
);
225 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
230 interface_do_reload(struct interface
*iface
)
232 interface_cleanup(iface
, true);
233 proto_init_interface(iface
, iface
->config
);
234 interface_claim_device(iface
);
238 interface_handle_config_change(struct interface
*iface
)
240 switch(iface
->config_state
) {
244 interface_do_reload(iface
);
247 interface_do_free(iface
);
250 if (iface
->autostart
&& iface
->available
)
251 interface_set_up(iface
);
255 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
257 struct interface
*iface
= state
->iface
;
261 if (iface
->state
!= IFS_SETUP
)
264 interface_ip_set_enabled(&iface
->config_ip
, true);
265 system_flush_routes();
266 iface
->state
= IFS_UP
;
267 iface
->start_time
= system_get_rtime();
268 interface_event(iface
, IFEV_UP
);
269 interface_write_resolv_conf();
270 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
273 if (iface
->state
== IFS_DOWN
)
276 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
277 interface_ip_set_enabled(&iface
->config_ip
, false);
278 system_flush_routes();
279 mark_interface_down(iface
);
280 interface_handle_config_change(iface
);
282 case IFPEV_LINK_LOST
:
283 if (iface
->state
!= IFS_UP
)
286 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
287 if (iface
->state
== IFS_UP
)
288 interface_event(iface
, IFEV_DOWN
);
289 iface
->state
= IFS_SETUP
;
294 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
297 iface
->proto
->free(iface
->proto
);
300 iface
->state
= IFS_DOWN
;
301 iface
->proto
= state
;
305 state
->proto_event
= interface_proto_cb
;
306 state
->iface
= iface
;
310 interface_init(struct interface
*iface
, const char *name
,
311 struct blob_attr
*config
)
313 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
314 struct blob_attr
*cur
;
315 const char *proto_name
= NULL
;
317 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
318 INIT_LIST_HEAD(&iface
->errors
);
319 INIT_LIST_HEAD(&iface
->users
);
320 INIT_LIST_HEAD(&iface
->hotplug_list
);
321 interface_ip_init(&iface
->proto_ip
, iface
);
322 interface_ip_init(&iface
->config_ip
, iface
);
323 iface
->config_ip
.enabled
= false;
325 iface
->main_dev
.cb
= interface_cb
;
326 iface
->l3_dev
= &iface
->main_dev
;
328 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
329 blob_data(config
), blob_len(config
));
331 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
332 proto_name
= blobmsg_data(cur
);
334 proto_attach_interface(iface
, proto_name
);
336 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
337 iface
->proto_ip
.no_defaultroute
=
338 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
340 iface
->config_autostart
= iface
->autostart
;
344 interface_add(struct interface
*iface
, struct blob_attr
*config
)
346 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
347 struct blob_attr
*cur
;
349 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
350 blob_data(config
), blob_len(config
));
352 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
353 iface
->ifname
= blobmsg_data(cur
);
355 iface
->config
= config
;
356 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
360 interface_remove_link(struct interface
*iface
, struct device
*dev
)
362 struct device
*mdev
= iface
->main_dev
.dev
;
364 if (mdev
&& mdev
->hotplug_ops
)
365 return mdev
->hotplug_ops
->del(mdev
, dev
);
367 if (!iface
->main_dev
.hotplug
)
368 return UBUS_STATUS_INVALID_ARGUMENT
;
370 if (dev
!= iface
->main_dev
.dev
)
371 return UBUS_STATUS_INVALID_ARGUMENT
;
373 device_remove_user(&iface
->main_dev
);
378 interface_add_link(struct interface
*iface
, struct device
*dev
)
380 struct device
*mdev
= iface
->main_dev
.dev
;
385 if (iface
->main_dev
.hotplug
)
386 device_remove_user(&iface
->main_dev
);
389 if (mdev
->hotplug_ops
)
390 return mdev
->hotplug_ops
->add(mdev
, dev
);
392 return UBUS_STATUS_NOT_SUPPORTED
;
395 device_add_user(&iface
->main_dev
, dev
);
396 iface
->main_dev
.hotplug
= true;
401 interface_set_up(struct interface
*iface
)
405 iface
->autostart
= true;
407 if (iface
->state
!= IFS_DOWN
)
410 interface_clear_errors(iface
);
411 if (!iface
->available
) {
412 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
416 if (iface
->main_dev
.dev
) {
417 ret
= device_claim(&iface
->main_dev
);
422 iface
->state
= IFS_SETUP
;
423 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
425 mark_interface_down(iface
);
433 interface_set_down(struct interface
*iface
)
436 vlist_for_each_element(&interfaces
, iface
, node
)
437 __interface_set_down(iface
, false);
439 iface
->autostart
= false;
440 __interface_set_down(iface
, false);
447 interface_start_pending(void)
449 struct interface
*iface
;
451 vlist_for_each_element(&interfaces
, iface
, node
) {
452 if (iface
->available
&& iface
->autostart
)
453 interface_set_up(iface
);
458 set_config_state(struct interface
*iface
, enum interface_config_state s
)
460 iface
->config_state
= s
;
461 if (iface
->state
== IFS_DOWN
)
462 interface_handle_config_change(iface
);
464 __interface_set_down(iface
, false);
468 interface_update_start(struct interface
*iface
)
470 interface_ip_update_start(&iface
->proto_ip
);
474 interface_update_complete(struct interface
*iface
)
476 struct device_route
*route
;
478 interface_ip_update_complete(&iface
->proto_ip
);
479 vlist_for_each_element(&iface
->config_ip
.route
, route
, node
) {
480 if (iface
->l3_dev
->dev
) {
481 system_add_route(iface
->l3_dev
->dev
, route
);
482 route
->enabled
= true;
488 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
490 struct blob_attr
*old_config
= if_old
->config
;
491 const char *old_ifname
= if_old
->ifname
;
492 const struct proto_handler
*proto
= if_old
->proto_handler
;
494 interface_clear_errors(if_old
);
495 if_old
->config
= if_new
->config
;
496 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
497 if_old
->autostart
= true;
499 if_old
->config_autostart
= if_new
->config_autostart
;
500 if_old
->ifname
= if_new
->ifname
;
501 if_old
->proto_handler
= if_new
->proto_handler
;
503 if ((!!old_ifname
!= !!if_new
->ifname
) ||
504 (old_ifname
&& strcmp(old_ifname
, if_new
->ifname
) != 0) ||
505 proto
!= if_new
->proto_handler
) {
506 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
511 if (!proto
->config_params
)
512 D(INTERFACE
, "No config parameters for interface '%s'\n",
514 else if (!config_check_equal(old_config
, if_new
->config
,
515 proto
->config_params
)) {
516 D(INTERFACE
, "Reload interface '%s because of config changes\n",
521 #define UPDATE(field) ({ \
522 bool __changed = (if_old->field != if_new->field); \
523 if_old->field = if_new->field; \
527 if (UPDATE(metric
) || UPDATE(proto_ip
.no_defaultroute
)) {
528 interface_ip_set_enabled(&if_old
->config_ip
, false);
529 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
530 interface_ip_set_enabled(&if_old
->proto_ip
, false);
531 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
539 set_config_state(if_old
, IFC_RELOAD
);
546 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
547 struct vlist_node
*node_old
)
549 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
550 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
552 if (node_old
&& node_new
) {
553 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
554 interface_change_config(if_old
, if_new
);
555 } else if (node_old
) {
556 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
557 set_config_state(if_old
, IFC_REMOVE
);
558 } else if (node_new
) {
559 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
560 proto_init_interface(if_new
, if_new
->config
);
561 interface_claim_device(if_new
);
562 netifd_ubus_add_interface(if_new
);
568 interface_init_list(void)
570 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
571 interfaces
.keep_old
= true;
572 interfaces
.no_delete
= true;