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_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
111 interface_cb(struct device_user
*dep
, enum device_event ev
)
113 struct interface
*iface
;
116 iface
= container_of(dep
, struct interface
, main_dev
);
121 case DEV_EVENT_REMOVE
:
128 if (iface
->active
== new_state
)
131 iface
->active
= new_state
;
134 if (iface
->autostart
&& !config_init
)
135 set_interface_up(iface
);
137 __set_interface_down(iface
, true);
141 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
143 struct interface
*iface
= state
->iface
;
147 if (iface
->state
!= IFS_SETUP
)
150 iface
->state
= IFS_UP
;
151 interface_event(iface
, IFEV_UP
);
154 if (iface
->state
== IFS_DOWN
)
157 mark_interface_down(iface
);
162 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
165 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, true);
166 iface
->proto
->free(iface
->proto
);
169 iface
->state
= IFS_DOWN
;
170 iface
->proto
= state
;
174 state
->proto_event
= interface_proto_cb
;
175 state
->iface
= iface
;
179 alloc_interface(const char *name
, struct uci_section
*s
)
181 struct interface
*iface
;
183 iface
= get_interface(name
);
187 iface
= calloc(1, sizeof(*iface
));
188 iface
->main_dev
.cb
= interface_cb
;
189 iface
->l3_iface
= &iface
->main_dev
;
190 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
191 list_add(&iface
->list
, &interfaces
);
192 INIT_LIST_HEAD(&iface
->errors
);
193 INIT_LIST_HEAD(&iface
->address
);
194 INIT_LIST_HEAD(&iface
->routes
);
196 proto_attach_interface(iface
, s
);
198 netifd_ubus_add_interface(iface
);
204 free_interface(struct interface
*iface
)
206 netifd_ubus_remove_interface(iface
);
207 list_del(&iface
->list
);
208 if (iface
->proto
->free
)
209 iface
->proto
->free(iface
->proto
);
214 get_interface(const char *name
)
216 struct interface
*iface
;
218 list_for_each_entry(iface
, &interfaces
, list
) {
219 if (!strcmp(iface
->name
, name
))
226 interface_remove_link(struct interface
*iface
, struct device
*dev
)
228 struct device
*mdev
= iface
->main_dev
.dev
;
230 if (mdev
&& mdev
->hotplug_ops
) {
231 mdev
->hotplug_ops
->del(mdev
, dev
);
235 remove_device_user(&iface
->main_dev
);
239 interface_add_link(struct interface
*iface
, struct device
*dev
)
241 struct device
*mdev
= iface
->main_dev
.dev
;
243 if (mdev
&& mdev
->hotplug_ops
)
244 return mdev
->hotplug_ops
->add(mdev
, dev
);
246 if (iface
->main_dev
.dev
)
247 interface_remove_link(iface
, NULL
);
249 add_device_user(&iface
->main_dev
, dev
);
255 set_interface_up(struct interface
*iface
)
257 iface
->autostart
= true;
259 if (!iface
->active
) {
260 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
264 if (iface
->state
!= IFS_DOWN
)
267 return __set_interface_up(iface
);
271 set_interface_down(struct interface
*iface
)
273 iface
->autostart
= false;
274 __set_interface_down(iface
, false);
280 start_pending_interfaces(void)
282 struct interface
*iface
;
284 list_for_each_entry(iface
, &interfaces
, list
) {
285 if (iface
->active
&& iface
->autostart
)
286 set_interface_up(iface
);