9 static LIST_HEAD(interfaces
);
12 clear_interface_errors(struct interface
*iface
)
14 struct interface_error
*error
, *tmp
;
16 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
17 list_del(&error
->list
);
22 void interface_add_error(struct interface
*iface
, const char *subsystem
,
23 const char *code
, const char **data
, int n_data
)
25 struct interface_error
*error
;
31 len
= n_data
* sizeof(char *);
32 datalen
= alloca(len
);
33 for (i
= 0; i
< n_data
; i
++) {
34 datalen
[i
] = strlen(data
[i
]) + 1;
39 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
43 list_add_tail(&error
->list
, &iface
->errors
);
44 error
->subsystem
= subsystem
;
47 dest
= (char *) &error
->data
[n_data
+ 1];
48 for (i
= 0; i
< n_data
; i
++) {
49 error
->data
[i
] = dest
;
50 memcpy(dest
, data
[i
], datalen
[i
]);
53 error
->data
[n_data
] = NULL
;
57 interface_event(struct interface
*iface
, enum interface_event ev
)
63 __set_interface_up(struct interface
*iface
)
67 if (iface
->state
!= IFS_DOWN
)
70 ret
= claim_device(iface
->main_dev
.dev
);
74 iface
->state
= IFS_SETUP
;
75 ret
= iface
->proto
->handler(iface
->proto
, PROTO_CMD_SETUP
, false);
82 release_device(iface
->main_dev
.dev
);
84 iface
->state
= IFS_DOWN
;
89 __set_interface_down(struct interface
*iface
, bool force
)
91 clear_interface_errors(iface
);
93 if (iface
->state
== IFS_DOWN
||
94 iface
->state
== IFS_TEARDOWN
)
97 iface
->state
= IFS_TEARDOWN
;
98 interface_event(iface
, IFEV_DOWN
);
100 iface
->proto
->handler(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
101 release_device(iface
->main_dev
.dev
);
105 interface_cb(struct device_user
*dep
, enum device_event ev
)
107 struct interface
*iface
;
110 iface
= container_of(dep
, struct interface
, main_dev
);
115 case DEV_EVENT_REMOVE
:
122 if (iface
->active
== new_state
)
125 iface
->active
= new_state
;
128 if (iface
->autostart
)
129 set_interface_up(iface
);
131 __set_interface_down(iface
, true);
135 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
137 struct interface
*iface
= state
->iface
;
141 if (iface
->state
!= IFS_SETUP
)
144 iface
->state
= IFS_UP
;
145 interface_event(iface
, IFEV_UP
);
148 iface
->state
= IFS_DOWN
;
153 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
156 iface
->proto
->handler(iface
->proto
, PROTO_CMD_TEARDOWN
, true);
157 iface
->proto
->free(iface
->proto
);
160 iface
->state
= IFS_DOWN
;
161 iface
->proto
= state
;
165 state
->proto_event
= interface_proto_cb
;
166 state
->iface
= iface
;
170 alloc_interface(const char *name
)
172 struct interface
*iface
;
174 iface
= get_interface(name
);
178 iface
= calloc(1, sizeof(*iface
));
180 interface_set_proto_state(iface
, get_default_proto());
186 iface
->main_dev
.cb
= interface_cb
;
187 iface
->l3_iface
= &iface
->main_dev
;
188 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
189 list_add(&iface
->list
, &interfaces
);
190 INIT_LIST_HEAD(&iface
->errors
);
192 netifd_ubus_add_interface(iface
);
198 free_interface(struct interface
*iface
)
200 netifd_ubus_remove_interface(iface
);
201 list_del(&iface
->list
);
202 if (iface
->proto
->free
)
203 iface
->proto
->free(iface
->proto
);
208 get_interface(const char *name
)
210 struct interface
*iface
;
212 list_for_each_entry(iface
, &interfaces
, list
) {
213 if (!strcmp(iface
->name
, name
))
220 interface_remove_link(struct interface
*iface
, struct device
*llif
)
222 struct device
*dev
= iface
->main_dev
.dev
;
224 if (dev
&& dev
->hotplug_ops
) {
225 dev
->hotplug_ops
->del(dev
, llif
);
229 remove_device_user(&iface
->main_dev
);
233 interface_add_link(struct interface
*iface
, struct device
*llif
)
235 struct device
*dev
= iface
->main_dev
.dev
;
237 if (dev
&& dev
->hotplug_ops
)
238 return dev
->hotplug_ops
->add(dev
, llif
);
240 if (iface
->main_dev
.dev
)
241 interface_remove_link(iface
, NULL
);
243 add_device_user(&iface
->main_dev
, llif
);
249 set_interface_up(struct interface
*iface
)
251 iface
->autostart
= true;
253 if (!iface
->active
) {
254 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
258 if (iface
->state
!= IFS_DOWN
)
261 return __set_interface_up(iface
);
265 set_interface_down(struct interface
*iface
)
267 iface
->autostart
= false;
268 __set_interface_down(iface
, false);