8 #include "interface-ip.h"
14 struct vlist_tree interfaces
;
15 static LIST_HEAD(iface_all_users
);
21 IFACE_ATTR_DEFAULTROUTE
,
26 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
27 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
28 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
29 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
30 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
31 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
34 const struct config_param_list interface_attr_list
= {
35 .n_params
= IFACE_ATTR_MAX
,
36 .params
= iface_attrs
,
40 interface_clear_errors(struct interface
*iface
)
42 struct interface_error
*error
, *tmp
;
44 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
45 list_del(&error
->list
);
50 void interface_add_error(struct interface
*iface
, const char *subsystem
,
51 const char *code
, const char **data
, int n_data
)
53 struct interface_error
*error
;
59 len
= n_data
* sizeof(char *);
60 datalen
= alloca(len
);
61 for (i
= 0; i
< n_data
; i
++) {
62 datalen
[i
] = strlen(data
[i
]) + 1;
67 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
71 list_add_tail(&error
->list
, &iface
->errors
);
72 error
->subsystem
= subsystem
;
75 dest
= (char *) &error
->data
[n_data
+ 1];
76 for (i
= 0; i
< n_data
; i
++) {
77 error
->data
[i
] = dest
;
78 memcpy(dest
, data
[i
], datalen
[i
]);
81 error
->data
[n_data
] = NULL
;
85 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
87 avl_delete(&iface
->data
, &data
->node
);
92 interface_data_flush(struct interface
*iface
)
94 struct interface_data
*d
, *tmp
;
96 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
97 interface_data_del(iface
, d
);
101 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
103 struct interface_data
*n
, *o
;
105 if (!blobmsg_check_attr(data
, true))
106 return UBUS_STATUS_INVALID_ARGUMENT
;
108 n
= calloc(1, sizeof(*data
) + blob_pad_len(data
));
109 memcpy(n
->data
, data
, blob_pad_len(data
));
110 n
->node
.key
= blobmsg_name(data
);
112 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
114 interface_data_del(iface
, o
);
116 avl_insert(&iface
->data
, &n
->node
);
121 interface_event(struct interface
*iface
, enum interface_event ev
)
123 struct interface_user
*dep
, *tmp
;
125 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
126 dep
->cb(dep
, iface
, ev
);
128 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
129 dep
->cb(dep
, iface
, ev
);
131 interface_queue_event(iface
, ev
);
135 interface_flush_state(struct interface
*iface
)
137 if (iface
->main_dev
.dev
)
138 device_release(&iface
->main_dev
);
139 if (iface
->l3_dev
.dev
)
140 device_release(&iface
->l3_dev
);
141 interface_data_flush(iface
);
145 mark_interface_down(struct interface
*iface
)
147 if (iface
->state
== IFS_UP
)
148 interface_event(iface
, IFEV_DOWN
);
149 interface_ip_set_enabled(&iface
->config_ip
, false);
150 interface_ip_flush(&iface
->proto_ip
);
151 interface_flush_state(iface
);
152 system_flush_routes();
153 iface
->state
= IFS_DOWN
;
157 __interface_set_down(struct interface
*iface
, bool force
)
159 interface_clear_errors(iface
);
161 if (iface
->state
== IFS_DOWN
||
162 iface
->state
== IFS_TEARDOWN
)
165 if (iface
->state
== IFS_UP
)
166 interface_event(iface
, IFEV_DOWN
);
167 iface
->state
= IFS_TEARDOWN
;
168 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
170 interface_flush_state(iface
);
174 interface_cb(struct device_user
*dep
, enum device_event ev
)
176 struct interface
*iface
;
179 iface
= container_of(dep
, struct interface
, main_dev
);
184 case DEV_EVENT_REMOVE
:
191 interface_set_available(iface
, new_state
);
195 interface_set_available(struct interface
*iface
, bool new_state
)
197 if (iface
->available
== new_state
)
200 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
201 iface
->available
= new_state
;
204 if (iface
->autostart
&& !config_init
)
205 interface_set_up(iface
);
207 __interface_set_down(iface
, true);
211 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
214 list_add(&dep
->list
, &iface_all_users
);
219 list_add(&dep
->list
, &iface
->users
);
220 if (iface
->state
== IFS_UP
)
221 dep
->cb(dep
, iface
, IFEV_UP
);
225 interface_remove_user(struct interface_user
*dep
)
227 list_del_init(&dep
->list
);
232 interface_claim_device(struct interface
*iface
)
237 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
238 dev
= device_get(iface
->ifname
, true);
240 interface_set_main_dev(iface
, dev
);
242 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
243 interface_set_available(iface
, true);
248 interface_cleanup(struct interface
*iface
, bool reload
)
250 struct interface_user
*dep
, *tmp
;
252 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
253 interface_remove_user(dep
);
255 interface_dequeue_event(iface
);
256 interface_ip_flush(&iface
->config_ip
);
257 interface_flush_state(iface
);
258 interface_clear_errors(iface
);
259 if (iface
->main_dev
.dev
&&
260 (!reload
|| !iface
->main_dev
.hotplug
))
261 interface_set_main_dev(iface
, NULL
);
262 interface_set_proto_state(iface
, NULL
);
266 interface_do_free(struct interface
*iface
)
268 interface_event(iface
, IFEV_FREE
);
269 interface_cleanup(iface
, false);
271 netifd_ubus_remove_interface(iface
);
272 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
277 interface_do_reload(struct interface
*iface
)
279 interface_event(iface
, IFEV_RELOAD
);
280 interface_cleanup(iface
, true);
281 proto_init_interface(iface
, iface
->config
);
282 interface_claim_device(iface
);
286 interface_handle_config_change(struct interface
*iface
)
288 switch(iface
->config_state
) {
292 interface_do_reload(iface
);
295 interface_do_free(iface
);
298 if (iface
->autostart
&& iface
->available
)
299 interface_set_up(iface
);
303 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
305 struct interface
*iface
= state
->iface
;
309 if (iface
->state
!= IFS_SETUP
)
312 interface_ip_set_enabled(&iface
->config_ip
, true);
313 system_flush_routes();
314 iface
->state
= IFS_UP
;
315 iface
->start_time
= system_get_rtime();
316 interface_event(iface
, IFEV_UP
);
317 interface_write_resolv_conf();
318 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
321 if (iface
->state
== IFS_DOWN
)
324 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
325 mark_interface_down(iface
);
326 interface_handle_config_change(iface
);
328 case IFPEV_LINK_LOST
:
329 if (iface
->state
!= IFS_UP
)
332 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
333 mark_interface_down(iface
);
334 iface
->state
= IFS_SETUP
;
339 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
342 iface
->proto
->free(iface
->proto
);
345 iface
->state
= IFS_DOWN
;
346 iface
->proto
= state
;
350 state
->proto_event
= interface_proto_cb
;
351 state
->iface
= iface
;
355 interface_init(struct interface
*iface
, const char *name
,
356 struct blob_attr
*config
)
358 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
359 struct blob_attr
*cur
;
360 const char *proto_name
= NULL
;
362 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
363 INIT_LIST_HEAD(&iface
->errors
);
364 INIT_LIST_HEAD(&iface
->users
);
365 INIT_LIST_HEAD(&iface
->hotplug_list
);
366 interface_ip_init(iface
);
367 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
368 iface
->config_ip
.enabled
= false;
370 iface
->main_dev
.cb
= interface_cb
;
372 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
373 blob_data(config
), blob_len(config
));
375 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
376 proto_name
= blobmsg_data(cur
);
378 proto_attach_interface(iface
, proto_name
);
380 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
381 iface
->proto_ip
.no_defaultroute
=
382 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
384 iface
->config_autostart
= iface
->autostart
;
388 interface_add(struct interface
*iface
, struct blob_attr
*config
)
390 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
391 struct blob_attr
*cur
;
393 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
394 blob_data(config
), blob_len(config
));
396 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
397 iface
->ifname
= blobmsg_data(cur
);
399 iface
->config
= config
;
400 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
404 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
406 bool enabled
= iface
->config_ip
.enabled
;
407 bool claimed
= iface
->l3_dev
.claimed
;
409 if (iface
->l3_dev
.dev
== dev
)
412 interface_ip_set_enabled(&iface
->config_ip
, false);
413 interface_ip_flush(&iface
->proto_ip
);
414 device_add_user(&iface
->l3_dev
, dev
);
418 device_claim(&iface
->l3_dev
);
419 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
424 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
426 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
427 bool claimed
= iface
->l3_dev
.claimed
;
429 if (iface
->main_dev
.dev
== dev
)
433 interface_set_l3_dev(iface
, dev
);
435 device_add_user(&iface
->main_dev
, dev
);
437 device_claim(&iface
->l3_dev
);
439 if (!iface
->l3_dev
.dev
)
440 interface_set_l3_dev(iface
, dev
);
444 interface_remove_link(struct interface
*iface
, struct device
*dev
)
446 struct device
*mdev
= iface
->main_dev
.dev
;
448 if (mdev
&& mdev
->hotplug_ops
)
449 return mdev
->hotplug_ops
->del(mdev
, dev
);
451 if (!iface
->main_dev
.hotplug
)
452 return UBUS_STATUS_INVALID_ARGUMENT
;
454 if (dev
!= iface
->main_dev
.dev
)
455 return UBUS_STATUS_INVALID_ARGUMENT
;
457 device_remove_user(&iface
->main_dev
);
462 interface_add_link(struct interface
*iface
, struct device
*dev
)
464 struct device
*mdev
= iface
->main_dev
.dev
;
469 if (iface
->main_dev
.hotplug
)
470 device_remove_user(&iface
->main_dev
);
473 if (mdev
->hotplug_ops
)
474 return mdev
->hotplug_ops
->add(mdev
, dev
);
476 return UBUS_STATUS_NOT_SUPPORTED
;
479 interface_set_main_dev(iface
, dev
);
480 iface
->main_dev
.hotplug
= true;
485 interface_set_up(struct interface
*iface
)
489 iface
->autostart
= true;
491 if (iface
->state
!= IFS_DOWN
)
494 interface_clear_errors(iface
);
495 if (!iface
->available
) {
496 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
500 if (iface
->main_dev
.dev
) {
501 ret
= device_claim(&iface
->main_dev
);
506 iface
->state
= IFS_SETUP
;
507 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
509 mark_interface_down(iface
);
517 interface_set_down(struct interface
*iface
)
520 vlist_for_each_element(&interfaces
, iface
, node
)
521 __interface_set_down(iface
, false);
523 iface
->autostart
= false;
524 __interface_set_down(iface
, false);
531 interface_start_pending(void)
533 struct interface
*iface
;
535 vlist_for_each_element(&interfaces
, iface
, node
) {
536 if (iface
->available
&& iface
->autostart
)
537 interface_set_up(iface
);
542 set_config_state(struct interface
*iface
, enum interface_config_state s
)
544 iface
->config_state
= s
;
545 if (iface
->state
== IFS_DOWN
)
546 interface_handle_config_change(iface
);
548 __interface_set_down(iface
, false);
552 interface_update_start(struct interface
*iface
)
554 interface_ip_update_start(&iface
->proto_ip
);
558 interface_update_complete(struct interface
*iface
)
560 interface_ip_update_complete(&iface
->proto_ip
);
564 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
566 struct blob_attr
*old_config
= if_old
->config
;
567 const char *old_ifname
= if_old
->ifname
;
568 const struct proto_handler
*proto
= if_old
->proto_handler
;
570 interface_clear_errors(if_old
);
571 if_old
->config
= if_new
->config
;
572 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
573 if_old
->autostart
= true;
575 if_old
->config_autostart
= if_new
->config_autostart
;
576 if_old
->ifname
= if_new
->ifname
;
577 if_old
->proto_handler
= if_new
->proto_handler
;
579 if ((!!old_ifname
!= !!if_new
->ifname
) ||
580 (old_ifname
&& strcmp(old_ifname
, if_new
->ifname
) != 0) ||
581 proto
!= if_new
->proto_handler
) {
582 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
587 if (!proto
->config_params
)
588 D(INTERFACE
, "No config parameters for interface '%s'\n",
590 else if (!config_check_equal(old_config
, if_new
->config
,
591 proto
->config_params
)) {
592 D(INTERFACE
, "Reload interface '%s because of config changes\n",
597 #define UPDATE(field) ({ \
598 bool __changed = (if_old->field != if_new->field); \
599 if_old->field = if_new->field; \
603 if (UPDATE(metric
) || UPDATE(proto_ip
.no_defaultroute
)) {
604 interface_ip_set_enabled(&if_old
->config_ip
, false);
605 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
606 interface_ip_set_enabled(&if_old
->proto_ip
, false);
607 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
615 set_config_state(if_old
, IFC_RELOAD
);
622 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
623 struct vlist_node
*node_old
)
625 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
626 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
628 if (node_old
&& node_new
) {
629 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
630 interface_change_config(if_old
, if_new
);
631 } else if (node_old
) {
632 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
633 set_config_state(if_old
, IFC_REMOVE
);
634 } else if (node_new
) {
635 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
636 proto_init_interface(if_new
, if_new
->config
);
637 interface_claim_device(if_new
);
638 netifd_ubus_add_interface(if_new
);
644 interface_init_list(void)
646 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
647 interfaces
.keep_old
= true;
648 interfaces
.no_delete
= true;