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_data_del(struct interface
*iface
, struct interface_data
*data
)
86 avl_delete(&iface
->data
, &data
->node
);
91 interface_data_flush(struct interface
*iface
)
93 struct interface_data
*d
, *tmp
;
95 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
96 interface_data_del(iface
, d
);
100 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
102 struct interface_data
*n
, *o
;
104 if (!blobmsg_check_attr(data
, true))
105 return UBUS_STATUS_INVALID_ARGUMENT
;
107 n
= calloc(1, sizeof(*data
) + blob_pad_len(data
));
108 memcpy(n
->data
, data
, blob_pad_len(data
));
109 n
->node
.key
= blobmsg_name(data
);
111 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
113 interface_data_del(iface
, o
);
115 avl_insert(&iface
->data
, &n
->node
);
120 interface_event(struct interface
*iface
, enum interface_event ev
)
122 struct interface_user
*dep
, *tmp
;
124 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
125 dep
->cb(dep
, IFEV_UP
);
127 interface_queue_event(iface
, ev
);
131 interface_flush_state(struct interface
*iface
)
133 if (iface
->main_dev
.dev
)
134 device_release(&iface
->main_dev
);
135 if (iface
->l3_dev
.dev
)
136 device_release(&iface
->l3_dev
);
137 interface_data_flush(iface
);
141 mark_interface_down(struct interface
*iface
)
143 if (iface
->state
== IFS_UP
)
144 interface_event(iface
, IFEV_DOWN
);
145 interface_ip_set_enabled(&iface
->config_ip
, false);
146 interface_ip_flush(&iface
->proto_ip
);
147 interface_flush_state(iface
);
148 system_flush_routes();
149 iface
->state
= IFS_DOWN
;
153 __interface_set_down(struct interface
*iface
, bool force
)
155 interface_clear_errors(iface
);
157 if (iface
->state
== IFS_DOWN
||
158 iface
->state
== IFS_TEARDOWN
)
161 if (iface
->state
== IFS_UP
)
162 interface_event(iface
, IFEV_DOWN
);
163 iface
->state
= IFS_TEARDOWN
;
164 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
166 interface_flush_state(iface
);
170 interface_cb(struct device_user
*dep
, enum device_event ev
)
172 struct interface
*iface
;
175 iface
= container_of(dep
, struct interface
, main_dev
);
180 case DEV_EVENT_REMOVE
:
187 interface_set_available(iface
, new_state
);
191 interface_set_available(struct interface
*iface
, bool new_state
)
193 if (iface
->available
== new_state
)
196 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
197 iface
->available
= new_state
;
200 if (iface
->autostart
&& !config_init
)
201 interface_set_up(iface
);
203 __interface_set_down(iface
, true);
207 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
210 list_add(&dep
->list
, &iface
->users
);
211 if (iface
->state
== IFS_UP
)
212 dep
->cb(dep
, IFEV_UP
);
216 interface_remove_user(struct interface_user
*dep
)
218 list_del_init(&dep
->list
);
223 interface_claim_device(struct interface
*iface
)
228 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
229 dev
= device_get(iface
->ifname
, true);
231 interface_set_main_dev(iface
, dev
);
233 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
234 interface_set_available(iface
, true);
239 interface_cleanup(struct interface
*iface
, bool reload
)
241 struct interface_user
*dep
, *tmp
;
243 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
244 interface_remove_user(dep
);
246 interface_dequeue_event(iface
);
247 interface_ip_flush(&iface
->config_ip
);
248 interface_flush_state(iface
);
249 interface_clear_errors(iface
);
250 if (iface
->main_dev
.dev
&&
251 (!reload
|| !iface
->main_dev
.hotplug
))
252 interface_set_main_dev(iface
, NULL
);
253 interface_set_proto_state(iface
, NULL
);
257 interface_do_free(struct interface
*iface
)
259 interface_cleanup(iface
, false);
261 netifd_ubus_remove_interface(iface
);
262 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
267 interface_do_reload(struct interface
*iface
)
269 interface_cleanup(iface
, true);
270 proto_init_interface(iface
, iface
->config
);
271 interface_claim_device(iface
);
275 interface_handle_config_change(struct interface
*iface
)
277 switch(iface
->config_state
) {
281 interface_do_reload(iface
);
284 interface_do_free(iface
);
287 if (iface
->autostart
&& iface
->available
)
288 interface_set_up(iface
);
292 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
294 struct interface
*iface
= state
->iface
;
298 if (iface
->state
!= IFS_SETUP
)
301 interface_ip_set_enabled(&iface
->config_ip
, true);
302 system_flush_routes();
303 iface
->state
= IFS_UP
;
304 iface
->start_time
= system_get_rtime();
305 interface_event(iface
, IFEV_UP
);
306 interface_write_resolv_conf();
307 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
310 if (iface
->state
== IFS_DOWN
)
313 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
314 mark_interface_down(iface
);
315 interface_handle_config_change(iface
);
317 case IFPEV_LINK_LOST
:
318 if (iface
->state
!= IFS_UP
)
321 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
322 mark_interface_down(iface
);
323 iface
->state
= IFS_SETUP
;
328 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
331 iface
->proto
->free(iface
->proto
);
334 iface
->state
= IFS_DOWN
;
335 iface
->proto
= state
;
339 state
->proto_event
= interface_proto_cb
;
340 state
->iface
= iface
;
344 interface_init(struct interface
*iface
, const char *name
,
345 struct blob_attr
*config
)
347 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
348 struct blob_attr
*cur
;
349 const char *proto_name
= NULL
;
351 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
352 INIT_LIST_HEAD(&iface
->errors
);
353 INIT_LIST_HEAD(&iface
->users
);
354 INIT_LIST_HEAD(&iface
->hotplug_list
);
355 interface_ip_init(&iface
->proto_ip
, iface
);
356 interface_ip_init(&iface
->config_ip
, iface
);
357 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
358 iface
->config_ip
.enabled
= false;
360 iface
->main_dev
.cb
= interface_cb
;
362 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
363 blob_data(config
), blob_len(config
));
365 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
366 proto_name
= blobmsg_data(cur
);
368 proto_attach_interface(iface
, proto_name
);
370 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
371 iface
->proto_ip
.no_defaultroute
=
372 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
374 iface
->config_autostart
= iface
->autostart
;
378 interface_add(struct interface
*iface
, struct blob_attr
*config
)
380 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
381 struct blob_attr
*cur
;
383 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
384 blob_data(config
), blob_len(config
));
386 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
387 iface
->ifname
= blobmsg_data(cur
);
389 iface
->config
= config
;
390 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
394 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
396 bool enabled
= iface
->config_ip
.enabled
;
397 bool claimed
= iface
->l3_dev
.claimed
;
399 if (iface
->l3_dev
.dev
== dev
)
402 interface_ip_set_enabled(&iface
->config_ip
, false);
403 interface_ip_flush(&iface
->proto_ip
);
404 device_add_user(&iface
->l3_dev
, dev
);
408 device_claim(&iface
->l3_dev
);
409 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
414 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
416 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
417 bool claimed
= iface
->l3_dev
.claimed
;
419 if (iface
->main_dev
.dev
== dev
)
423 interface_set_l3_dev(iface
, dev
);
425 device_add_user(&iface
->main_dev
, dev
);
427 device_claim(&iface
->l3_dev
);
429 if (!iface
->l3_dev
.dev
)
430 interface_set_l3_dev(iface
, dev
);
434 interface_remove_link(struct interface
*iface
, struct device
*dev
)
436 struct device
*mdev
= iface
->main_dev
.dev
;
438 if (mdev
&& mdev
->hotplug_ops
)
439 return mdev
->hotplug_ops
->del(mdev
, dev
);
441 if (!iface
->main_dev
.hotplug
)
442 return UBUS_STATUS_INVALID_ARGUMENT
;
444 if (dev
!= iface
->main_dev
.dev
)
445 return UBUS_STATUS_INVALID_ARGUMENT
;
447 device_remove_user(&iface
->main_dev
);
452 interface_add_link(struct interface
*iface
, struct device
*dev
)
454 struct device
*mdev
= iface
->main_dev
.dev
;
459 if (iface
->main_dev
.hotplug
)
460 device_remove_user(&iface
->main_dev
);
463 if (mdev
->hotplug_ops
)
464 return mdev
->hotplug_ops
->add(mdev
, dev
);
466 return UBUS_STATUS_NOT_SUPPORTED
;
469 interface_set_main_dev(iface
, dev
);
470 iface
->main_dev
.hotplug
= true;
475 interface_set_up(struct interface
*iface
)
479 iface
->autostart
= true;
481 if (iface
->state
!= IFS_DOWN
)
484 interface_clear_errors(iface
);
485 if (!iface
->available
) {
486 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
490 if (iface
->main_dev
.dev
) {
491 ret
= device_claim(&iface
->main_dev
);
496 iface
->state
= IFS_SETUP
;
497 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
499 mark_interface_down(iface
);
507 interface_set_down(struct interface
*iface
)
510 vlist_for_each_element(&interfaces
, iface
, node
)
511 __interface_set_down(iface
, false);
513 iface
->autostart
= false;
514 __interface_set_down(iface
, false);
521 interface_start_pending(void)
523 struct interface
*iface
;
525 vlist_for_each_element(&interfaces
, iface
, node
) {
526 if (iface
->available
&& iface
->autostart
)
527 interface_set_up(iface
);
532 set_config_state(struct interface
*iface
, enum interface_config_state s
)
534 iface
->config_state
= s
;
535 if (iface
->state
== IFS_DOWN
)
536 interface_handle_config_change(iface
);
538 __interface_set_down(iface
, false);
542 interface_update_start(struct interface
*iface
)
544 interface_ip_update_start(&iface
->proto_ip
);
548 interface_update_complete(struct interface
*iface
)
550 interface_ip_update_complete(&iface
->proto_ip
);
554 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
556 struct blob_attr
*old_config
= if_old
->config
;
557 const char *old_ifname
= if_old
->ifname
;
558 const struct proto_handler
*proto
= if_old
->proto_handler
;
560 interface_clear_errors(if_old
);
561 if_old
->config
= if_new
->config
;
562 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
563 if_old
->autostart
= true;
565 if_old
->config_autostart
= if_new
->config_autostart
;
566 if_old
->ifname
= if_new
->ifname
;
567 if_old
->proto_handler
= if_new
->proto_handler
;
569 if ((!!old_ifname
!= !!if_new
->ifname
) ||
570 (old_ifname
&& strcmp(old_ifname
, if_new
->ifname
) != 0) ||
571 proto
!= if_new
->proto_handler
) {
572 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
577 if (!proto
->config_params
)
578 D(INTERFACE
, "No config parameters for interface '%s'\n",
580 else if (!config_check_equal(old_config
, if_new
->config
,
581 proto
->config_params
)) {
582 D(INTERFACE
, "Reload interface '%s because of config changes\n",
587 #define UPDATE(field) ({ \
588 bool __changed = (if_old->field != if_new->field); \
589 if_old->field = if_new->field; \
593 if (UPDATE(metric
) || UPDATE(proto_ip
.no_defaultroute
)) {
594 interface_ip_set_enabled(&if_old
->config_ip
, false);
595 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
596 interface_ip_set_enabled(&if_old
->proto_ip
, false);
597 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
605 set_config_state(if_old
, IFC_RELOAD
);
612 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
613 struct vlist_node
*node_old
)
615 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
616 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
618 if (node_old
&& node_new
) {
619 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
620 interface_change_config(if_old
, if_new
);
621 } else if (node_old
) {
622 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
623 set_config_state(if_old
, IFC_REMOVE
);
624 } else if (node_new
) {
625 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
626 proto_init_interface(if_new
, if_new
->config
);
627 interface_claim_device(if_new
);
628 netifd_ubus_add_interface(if_new
);
634 interface_init_list(void)
636 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
637 interfaces
.keep_old
= true;
638 interfaces
.no_delete
= true;