dd303efc569afe1b50b79b4d2b07343269c656d0
11 static LIST_HEAD(interfaces
);
14 clear_interface_errors(struct interface
*iface
)
16 struct interface_error
*error
, *tmp
;
18 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
19 list_del(&error
->list
);
24 void interface_add_error(struct interface
*iface
, const char *subsystem
,
25 const char *code
, const char **data
, int n_data
)
27 struct interface_error
*error
;
33 len
= n_data
* sizeof(char *);
34 datalen
= alloca(len
);
35 for (i
= 0; i
< n_data
; i
++) {
36 datalen
[i
] = strlen(data
[i
]) + 1;
41 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
45 list_add_tail(&error
->list
, &iface
->errors
);
46 error
->subsystem
= subsystem
;
49 dest
= (char *) &error
->data
[n_data
+ 1];
50 for (i
= 0; i
< n_data
; i
++) {
51 error
->data
[i
] = dest
;
52 memcpy(dest
, data
[i
], datalen
[i
]);
55 error
->data
[n_data
] = NULL
;
59 interface_event(struct interface
*iface
, enum interface_event ev
)
65 mark_interface_down(struct interface
*iface
)
67 interface_del_ctx_addr(iface
, NULL
);
68 release_device(iface
->main_dev
.dev
);
69 iface
->state
= IFS_DOWN
;
73 __set_interface_up(struct interface
*iface
)
77 if (iface
->state
!= IFS_DOWN
)
80 ret
= claim_device(iface
->main_dev
.dev
);
84 iface
->state
= IFS_SETUP
;
85 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
87 mark_interface_down(iface
);
96 __set_interface_down(struct interface
*iface
, bool force
)
98 clear_interface_errors(iface
);
100 if (iface
->state
== IFS_DOWN
||
101 iface
->state
== IFS_TEARDOWN
)
104 iface
->state
= IFS_TEARDOWN
;
105 interface_event(iface
, IFEV_DOWN
);
107 interface_del_all_routes(iface
);
108 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
112 interface_cb(struct device_user
*dep
, enum device_event ev
)
114 struct interface
*iface
;
117 iface
= container_of(dep
, struct interface
, main_dev
);
122 case DEV_EVENT_REMOVE
:
129 if (iface
->active
== new_state
)
132 iface
->active
= new_state
;
135 if (iface
->autostart
&& !config_init
)
136 set_interface_up(iface
);
138 __set_interface_down(iface
, true);
142 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
144 struct interface
*iface
= state
->iface
;
148 if (iface
->state
!= IFS_SETUP
)
151 iface
->state
= IFS_UP
;
152 interface_event(iface
, IFEV_UP
);
155 if (iface
->state
== IFS_DOWN
)
158 mark_interface_down(iface
);
163 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
166 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, true);
167 iface
->proto
->free(iface
->proto
);
170 iface
->state
= IFS_DOWN
;
171 iface
->proto
= state
;
175 state
->proto_event
= interface_proto_cb
;
176 state
->iface
= iface
;
180 alloc_interface(const char *name
, struct uci_section
*s
)
182 struct interface
*iface
;
184 iface
= get_interface(name
);
188 iface
= calloc(1, sizeof(*iface
));
189 iface
->main_dev
.cb
= interface_cb
;
190 iface
->l3_iface
= &iface
->main_dev
;
191 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
192 list_add(&iface
->list
, &interfaces
);
193 INIT_LIST_HEAD(&iface
->errors
);
194 INIT_LIST_HEAD(&iface
->address
);
195 INIT_LIST_HEAD(&iface
->routes
);
197 proto_attach_interface(iface
, s
);
199 netifd_ubus_add_interface(iface
);
205 free_interface(struct interface
*iface
)
207 netifd_ubus_remove_interface(iface
);
208 list_del(&iface
->list
);
209 if (iface
->proto
->free
)
210 iface
->proto
->free(iface
->proto
);
215 get_interface(const char *name
)
217 struct interface
*iface
;
219 list_for_each_entry(iface
, &interfaces
, list
) {
220 if (!strcmp(iface
->name
, name
))
227 interface_remove_link(struct interface
*iface
, struct device
*dev
)
229 struct device
*mdev
= iface
->main_dev
.dev
;
231 if (mdev
&& mdev
->hotplug_ops
) {
232 mdev
->hotplug_ops
->del(mdev
, dev
);
236 remove_device_user(&iface
->main_dev
);
240 interface_add_link(struct interface
*iface
, struct device
*dev
)
242 struct device
*mdev
= iface
->main_dev
.dev
;
244 if (mdev
&& mdev
->hotplug_ops
)
245 return mdev
->hotplug_ops
->add(mdev
, dev
);
247 if (iface
->main_dev
.dev
)
248 interface_remove_link(iface
, NULL
);
250 add_device_user(&iface
->main_dev
, dev
);
256 set_interface_up(struct interface
*iface
)
258 iface
->autostart
= true;
260 if (!iface
->active
) {
261 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
265 if (iface
->state
!= IFS_DOWN
)
268 return __set_interface_up(iface
);
272 set_interface_down(struct interface
*iface
)
274 iface
->autostart
= false;
275 __set_interface_down(iface
, false);
281 start_pending_interfaces(void)
283 struct interface
*iface
;
285 list_for_each_entry(iface
, &interfaces
, list
) {
286 if (iface
->active
&& iface
->autostart
)
287 set_interface_up(iface
);